The 22-year-old Farnese Vini rookie led out the bunch sprint at the close of a short and fast 149km stage to Vedelago, holding off the challenge from both Team Sky's Cavendish and Italy's Roberto Ferrari (Androni Giocattoli).
It is Guardini's maiden win on his debut Grand Tour.
The result was a blow to Britain’s Cavendish, who was looking for a fourth Giro stage win and maximum points to consolidate his slender lead in the red jersey points competition.
Cavendish had benefited from a solid lead out from team-mates Geraint Thomas and Bernard Eisel on the straight and flat finish, but Guardini showed experience beyond his years in beating the fastest man on two wheels at his own game.
The win was the biggest career scalp for Guardini, seen as "the future of Italian sprinting" but cruelly ridiculed in the Italian press this week for being second-to-last in the overall standings.
Stage 11 winner Ferrari took third place behind Cavendish, with Robert Hunter (Garmin-Barracuda) and Sebastian Haedo (Saxo Bank) completing the top five.
Thursday’s stage started in San Vito di Cadore under bright skies and witnessed a break inside the opening two kilometres. Italians Manuele Boaro (Saxo Bank), Pier Paolo De Negri (Farnese Vini) and Angelo Pagani (Colnago-CSF Inox), and Dutchman Stef Clement (Rabobank) built up a maximum lead of two minutes and 50 seconds on a largely downhill opening half of the stage – only to be reeled in ahead of the intermediate sprint at Cesiomaggiore.
Team Sky had controlled the chase, eager to ensure maximum points in the sprint for Cavendish, who led race leader Joaquim Rodriguez (Katusha) by just one point in the overnight red jersey standings.
Cavendish took the eight sprint points available 65km from the finish, before a counter-attack saw four more riders form a break off the front of the pack.
This second group featured Clement once again, as well as Mickael Delage (FDJ-BigMat) and serial escapees Martijn Keizer (Vacansoleil-DCM) and Olivier Kaisen (Lotto Belisol).
The quartet’s lead peaked at 1:05 and they were reeled in by the peloton inside the final 15 kilometres. Another Lotto rider, stage 12 winner Lars Bak of Belgium, rode clear and in pursuit of Delage, who had put in one last bid to stay clear.
Delage and Bak combined well but never saw their lead increase to more than 12 seconds. The pair were caught inside the final four kilometres, setting things up for the inevitable bunch sprint.
Saxo Bank and NetApp forced their way to the front and looked to have crowded out the Sky train. But Germany’s Eisel forced a gap going under the banner and peeled off, leaving team-mates Thomas and Cavendish on the front.
Guardini had other plans, however, and his early burst of pace from his idol Cavendish’s slipstream proved to be the winning move.
Cavendish’s second place gave the world champion 20 more points in the red jersey competition. He now leads Rodriguez by 29 points, with no further realistic chances for the Manxman to add to his tally before the race’s conclusion on Sunday with a time trial in Milan.
Rodriguez retained his pink jersey as race leader after there were no changes in the general classification. The Spaniard leads Canada’s Ryder Hesjedal (Garmin-Barracuda) by 30 seconds, with Italy’s Ivan Basso (Liquigas) third at 1:22.
Saturday’s 198km stage 19 from Treviso to Alpe di Pampeago sees a return to the high mountains and precedes the race’s queen stage on Sunday, which concludes on the summit of the fabled Stelvio climb – the highest of any Grand Tour.