Boasson Hagen (Dimension Data) was also edged out in a photo-finish in the opening stage by Caleb Ewan (Orica-Scott) and had already donned the leader's green jersey on the podium before the gut-wrenching decision was made.
Viviani gestured angrily to Boasson Hagen after crossing the line in second, feeling he had been impeded.
The Dimension Data rider had started his sprint down the middle but moved left, failing to leave Viviani space along the barriers.
Over 40 minutes after the stage finished - it was confirmed the Italian had won and also taken the green jersey because of the ten-second time bonus.
“Owain [Doull] put me on Boasson Hagen’s wheel in the final which was perfect and what we talked about this morning in our meeting, to be in second or third place going into the sprint," Viviani said.
“We have a really strong leadout, Tao [Geoghegan Hart] is the youngest, but he is one of the best talents in British cycling and he has done two days on the front for me.
"All the team deserve this win and I hope to win again in the next few days too."
Boasson Hagen, a two-time Tour of Britain winner, had been released by lead-out man Mark Renshaw with just under 200 metres left of the 211km route and held off the challenge of Viviani and Dylan Groenewegen (LottoNL-Jumbo).
"I know that I closed in Viviani but it wasn't on purpose”, Boasson Hagen said. "I was just sprinting for the line with my head down and when I looked up, I was too close on the left side with Viviani on my inside. It's really a pity after all the hard work the guys did today. However, we will keep fighting and try to go for it again tomorrow”
Groenewegen moved up to second after Boasson Hagen's misery and Fernando Gaviria (Quick-Step Floors) was third.
Several riders attacked, including a late effort from BMC's Philippe Gilbert, who was reeled in with 5km to go.