The Colombian sprinter was positioned a long way back with a kilometre to go, but was brought back into a perfect position by a strong Quick-Step Floors lead-out effort.
With 300m to go on the flat finish in Messina, Gaviria was perfectly positioned on the wheel of Irishman Sam Bennett (Bora-Hansgrohe) and when Bennett opened his sprint, Gaviria was able to come round the outside, comfortably taking the stage win ahead of Jakub Mareczko (Wilier Triestina) who edged out Bennett for second.
That result, together with his efforts to pick up points at the stage's two intermediate sprints, were also enough to move Gaviria into the purple points jersey, while team-mate Bob Jungels finished safely in the bunch to stay in pink.
How it happened
Stage five of the 2017 Giro d'Italia saw the riders faced with 159km from Pedara to Messina, with two riders, Maciej Paterski (CCC Sprandi Polkowice) and Evgeny Shalunov (Gazprom-Rusvelo) going clear from the gun.
Quick-Step Floors, looking after the pink jersey of Bob Jungels, showed little interest in chasing, allowing the duo out front to establish a lead of 3-30 which remained steady for much of the day.
The only classified climb of the day came after just 38km into the stage, with Shalunov taking maximum points ahead of Paterski, while Daniel Teklehaimanot (Dimension Data) clipped off the front of the bunch to take the one remaining point to marginally tighten his grip on the blue jersey.
It was a similar pattern at the first of the day's two intermediate sprints, where Paterski rolled across the line at the head of the break, while Fernando Gaviria (Quick-Step Floors) jumped out of the bunch to pick up eight points to narrow André Greipel's lead in the points classification.
This pattern was repeated a few kilometres later at the second intermediate sprint, with Paterski once again crossed the line first while Gaviria jumped out of the pack to take more points.
After a relatively easy day in the saddle, it took little time for the peloton to close in on the breakaway, bringing the gap below a minute for the first time with 20km to go as the sprinters' teams massed on the front.
The breakaway was caught just a few kilometres later, with Bora-Hansgrohe controlling the front of the bunch as the race entered Messina.
The GC contenders were also at the front trying to stay out of trouble, with Vincenzo Nibali (Bahrain-Merida) particularly prominent as he rode towards the stage finish in his home town.
Watch: Giro d'Italia preview - stages one to nine
With seven kilometres to go the pace really ramped up as a Luka Pibernik (Bahrain-Merida) accelerated hard out of a roundabout to open a small gap over the Katusha-Alpecin lead out train.
The Slovenian rider seemed to be putting a lot of effort with a lap still remaining of the 6.3km finish circuit, the reason for which became clear when he raised his arms in celebration as he crossed the line, mistakenly thinking that he'd won the stage.
The stage's comedy moment out of the way, Lotto-Soudal moved to the front of the bunch together with Quick-Step Floors, with the pink jersey being kept out of trouble on the technical circuit, and Gaviria also in a prime position.
But it was Luka Mezgec (Orica-Scott) who strung the race out in the finishing straight, an effort that should have made it hard for any poorly positioned sprinters to come up the outside.
However after being caught out of position, Gaviria had his lead-out men to thank as they brought him down the outside of the pack, dropping him off in a perfect position on the wheel of Bora-Hansgrohe's Sam Bennett.
And as the Irishman opened his sprint, Gaviria just had too much, coming around the Irishman to win by a couple of bike lengths, holding off the charge of a rapidly approaching Jakub Mareczko.
Giro d'Italia 2017, stage five: Pedara to Messina (159km)
1. Fernando Gaviria (Col) Quick-Step Floors, in 3-40-11
2. Jakub Mareczko (Ita) Wilier Triestina
3. Sam Bennett (Irl) Bora-Hansgrohe
4. André Greipel (Ger) Lotto Soudal
5. Phil Bauhaus (Ger) Team Sunweb
6. Kristian Sbaragli (Ita) Dimension Data
7. Ryan Gibbons (RSA) Dimension Data
8. Roberto Ferrari (Ita) UAE Team Emirates
9. Jasper Stuyven (Bel) Trek-Segafredo
10. Enrico Battaglin (Ita) Team LottoNl-Jumbo, all at same time
General classification after stage five
1. Bob Jungels (Lux) Quick-Step Floors, in 23-22-07
2. Geraint Thomas (GBr) Team Sky, at 6 secs
3. Adam Yates (GBr) Orica-Scott, at 10 secs
4. Domenico Pozzovivo (Ita) AG2R La Mondiale
5. Vincenzo Nibali (Ita) Bahrain-Merida
6. Nairo Quintana (Col) Movistar Team
7. Tom Dumoulin (Ned) Team Sunweb
8. Bauke Mollema (Ned) Trek-Segafredo
9. Tejay Van Garderen (USA) BMC Racing
1.0 Andrey Amador (CRc) Movistar, all at same time
Thank you for reading 20 articles this month* Join now for unlimited access
Enjoy your first month for just £1 / $1 / €1
*Read 5 free articles per month without a subscription
Join now for unlimited access
Try first month for just £1 / $1 / €1