Ewan (Mitchelton-Scott) was in a perfect position near the end of Thursday's shortened 121km stage from Glenelg to Victor Harbor but was shocked to see Viviani (Quick-Step Floors) speed past on the inside and there was nothing he could do.
"It was a really hot day but I managed to refresh myself until 200 metres to go," Viviani said. "My (new) team has done an amazing job and I thank them all.
"I've never won so early in a season and it's not easy to have a good condition in January but I've done some work on the track for that. I'm delighted to win the fourth bunch sprint of the year."
Despite the misstep, he continues to lead the Tour overall, 10 seconds ahead of Viviani, who has taken second place from Ewan's team-mate Daryl Impey.
“I’m pretty disappointed with that to be honest. I got an absolutely perfect lead out, My teammates did exactly what I asked them to do but I stuffed up at the end," he said. "There was a headwind and I waited for too long.
"It's our fourth sprint and there's a fourth different winner (which) shows how high the (level of) competition is.
"Mentally I can calm down in the next two days and I'll be back in a winning mood on the last day."
All the top sprinters in the Tour have won a race apiece this week - Peter Sagan (BORA-hansgrohe) at the classic on Sunday night, Andre Greipel (lotto-Soudal) in stage one and then Ewan and Viviani.
The stage was shortened by 26km because of extreme 40-plus degree conditions.
UniSA Australia's Scott Bowden, who has been in all of the breakaways through the first three days of the race, braved the furnace to again spend nearly half of the stage in the lead.
He went clear with Nicholas Dlamini (Dimension Data) soon after the start at the coastal suburb of Glenelg.
Dlamini beat Bowden at the king of the mountain intermediate checkpoint, extending his lead in that category, and dropped back to the peloton halfway through the stage.
Two finishing laps at Victor Harbor were cut because of the brutal conditions.
The only saving grace for competitors was that they were riding with a tailwind, although without any real relief.
Bowden was caught with 19.5km left and, after a brief attack from Tiago Machado (Katusha), the sprinters set up for the finish.
Ewan won at Victor Harbor a year ago and was chasing his eighth stage win at the Tour.