Spain’s Joaquim Rodriguez retained the overall leader’s pink jersey with three days of racing to go.
Cavendish was followed home after the 149km run by Roberto Ferrari with South African Robert Hunter in fourth.
Cavendish, who is targeting the red jersey as the Tour’s overall points champion, narrowly missed out on his fourth stage of the race.
The Team Sky star will not be favoured by the make-up of the last three stages, two in the mountains and the concluding time trial.
He won the intermediate sprint on Thursday at the 84km mark.
In the closing stages the Isle of Man-born rider’s team-mates were unable to set him up for victory with Guardini launching his sprint to the line just ahead of Cavendish.
Guardini, who won six stages on the Tour of Lankawi in Malaysia at the start of the season, clinched his seventh success of the year.
“Incredible, incredible,” said the ecstatic winner who had finished plum last in Wednesday’s 17th stage.
Rodriguez meanwhile was wary of the threat posed by Canadian Ryder Hesjedal, who is lying half a minute behind in second in the overall standings. “Thirty seconds in front of him isn’t enough,” said Rodriguez.
“At the moment I see Hesjedal as the number one favourite,” added his Catalan team’s sporting director Valerio Piva. To win the Giro, Rodriguez must take time from the Canadian and maintain his cushion over the others.”
Italian favourite Ivan Basso is placed third overall, at 1min 22sec.
Friday’s stamina-sapping 19th stage in the mountains is a 198km run from Treviso to the Alpe di Pampeago.
Stage 18 Results:
1. Andrea Guardini (Italy / Farnese Vini) 3:00:52″ 2. Mark Cavendish (Britain / Team Sky) same time 3. Roberto Ferrari (Italy / Androni Giocattoli) 4. Robert Hunter (South Africa / Garmin) 5. Sebastian Haedo (Argentina / Saxo Bank) 6. Giacomo Nizzolo (Italy / RadioShack) 7. Alexander Kristoff (Norway / Katusha) 8. Francesco Chicchi (Italy / Omega Pharma – Quick-Step) 9. Geoffrey Soupe (France / FDJ) 10. Dennis Vanendert (Belgium / Lotto) 11. Fabio Sabatini (Italy / Liquigas) 12. Danilo Wyss (Switzerland / BMC Racing) 13. Fumiyuki Beppu (Japan / Orica) 14. Jens Keukeleire (Belgium / Orica) 15. Ben Gastauer (Luxembourg / AG2R)
1. Joaquim Rodriguez (Spain / Katusha) 77:47:38″ 2. Ryder Hesjedal (Canada / Garmin) +30″ 3. Ivan Basso (Italy / Liquigas) +1:22″ 4. Michele Scarponi (Italy / Lampre) +1:38″ 5. Rigoberto Uran (Colombia / Team Sky) +2:56″ 6. Benat Intxausti (Spain / Movistar) +3:04″ 7. Domenico Pozzovivo (Italy / Colnago – CSF) +3:19″ 8. Paolo Tiralongo (Italy / Astana) +4:13″ 9. Thomas De Gendt (Belgium / Vacansoleil) +4:38″ 10. Sergio Henao (Colombia / Team Sky) +4:42″ 11. Johann Tschopp (Switzerland / BMC Racing) +4:46″ 12. John Gadret (France / AG2R) 13. Daniel Moreno (Spain / Katusha) +4:55″ 14. Dario Cataldo (Italy / Omega Pharma – Quick-Step) +4:59″ 15. Damiano Cunego (Italy / Lampre) +5:07″