VILLAFRANCA TIRRENA, ITALY -- French rider Arnaud Demare won the first mass sprint of this year's Giro d'Italia in a three-way photo finish on Tuesday, and Portuguese rider João Almeida added a couple of seconds to his overall lead in the fourth stage.

Demare finished just ahead of Peter Sagan and Davide Ballerini.

"I think it was a millimeter," Demare said. "I had luck on my side. ... I often train for the last surge behind my dad's scooter. Today it paid off."

Geraint Thomas, the 2018 Tour de France champion, withdrew from the race before the stage after fracturing his pelvis in a fall on Monday. Thomas crashed in the neutral stage before Stage 3 began after getting a water bottle caught in his front wheel.

Demare, who rides for the Groupama-FDJ team, required nearly 3 1/2 hours to complete the 140-kilometre leg from Catania to Villafranca Tirrena, which featured one third-category climb midway along the route before a flat finish.

Sagan's Bora-Hansgrohe team set a blistering pace in the finale, leaving Fernando Gaviria and other top sprinters behind. Sagan, the three-time world champion who is competing in his first Giro, also finished second in Stage 2, which was won by Diego Ulissi.

Ottawa's Alex Cataford of Team Israel Start-Up Nation was 125th in Tuesday's stage and 96th overall.

It was Demare's second career win at the Giro. He won a stage in Modena last year to go with his two stage victories at the Tour de France in 2017 and 2018.

Almeida earned a couple of bonus seconds by finishing second in an intermediate sprint. He now leads Jonathan Caicedo, the Stage 3 winner atop Mount Etna, by two seconds.

"It was a bit stressful because I had to sprint for the bonus seconds," Almeida said. "Then I could coast to the finish."

The finishing straight was positioned alongside the coast and Vini Zabu teammates Luca Wackermann and Etienne van Empel crashed hard when wind coming in from the sea blew a barrier across the road.

The race crosses over to mainland Italy for Stage 5 on Wednesday, a 225-kilometre leg from Mileto to Camigliatello in Calabria.

The Giro was rescheduled from its usual May slot because of the coronavirus pandemic. The race ends on Oct. 25 with an individual time trial in Milan.