A superb leadout train from Team Sky had left Viviani ideally placed heading into the final hundred metres, but Boasson Hagen initially looked to have pipped him to the post.
However, the Norwegian was adjudged to have substantially deviated from his racing line in the sprint and he was relegated, placing Viviani atop the podium on the stage, but also earning him the green leader’s jersey.
Earlier on, Matt Holmes (Madison Genesis) initiated the breakaway as the flag dropped, which swelled to seven riders who stayed away for the majority of the stage, with Homes fittingly the last man caught by the chasing bunch with 14km to go.
Tao Geoghegan Hart had been instrumental in bringing the escapees back and looked to have set up a sprint finish, but Phillipe GIlbert (Quick-Step Floors) upset the sprinters by attacking with 8km remaining.
Strong work on the front of the bunch from Geraint Thomas and Vasil Kiryienka denied the Belgian a canny counter-attack win to bring the peloton back together and set up a sprint finish.
Owain Doull perfectly led out Viviani, who looked set to continue his excellent form with another win only for Boasson Hagen to deny him on the line after changing his line.