Viviani came out of Sagan's wheel after a chaotic, unorganised sprint finale to take his first ever win in home race Tirreno-Adriatico, with Sagan just squeezing in front of Fernando Gaviria (UAE Team Emirates) to take third.
“It’s been a pretty quiet stage," said Viviani. "Sometimes, breakaway riders take up to a 12-minute lead. Today we made a deal straight away with UAE Team Emirates so we controlled the race with one rider each.
"The finish was technical but the last curve wasn’t dangerous. Peter Sagan launched the sprint sitting on his saddle. He’s not a pure sprinter but, after 230km of racing, he can beat any of us.
"Fortunately, I still had some extra speed that we’re never sure to have left at the end of such a long race. This is my first race on Italian soil wearing the tricolour (Italian national champion's) jersey so I’m enjoying every moment even more.”
Sagan's second was made more impressive as he has been recovering from a stomach virus and hadn't had a chance to build much form. Viviani paid tribute to his rival's performance.
"He's the guy to watch because he's got so much class compared to the others," said Viviani. "He had trouble, and you could see from his body.
"He's lost weight but if you see how he moved in the last few kilometres, it's unbelievable. I'm really happy to win. It's never easy."
Stepan Kurianov (Gazprom-RusVelo), Natnael Berhane (Cofidis), Mirco Maestri (Bardiani-CSF) and Sebastian Schönberger (Neri Sottoli-Selle Italia) got the day of racing underway on what was expected to be a day for the sprinters. Alessandro Tonelli (Bardiani-CSF) and Alexander Cataford (Israel Cycling Academy) also made it across to the breakaway after bridging successfully.
Mitchelton-Scott did some work pegging the escapees' advantage in service of race leader Adam Yates, but the main work of bring the break back was left to the teams of the sprinters. The catch was made easily in the end, with Schönberger the last man caught as the race heated up in the final kilometres.
The leadout trains were thrown into disarray by a surge from Dimension Data and a subsequent swinging off from Sagan, which allowed Deceuninck-QuickStep to get back into the race and deliver Viviani into a position where he could sprint for the win.
Sagan launched first after coming round the final corner, but Viviani had too much power and was able to come round the three-time world champion and win comfortably in the end.