Guardini, who finished last in the previous stage, timed his sprint to perfection to win in three hours 52 seconds. Roberto Ferrari was third.
Cavendish could not keep up with the Italian at the end of the mainly flat and descending 149-kilometre route from San Vito di Cadore to Vedelago.
"It's a great day, I managed to do what I wanted to do," Guardini said. "I wanted to anticipate the sprint because I had to do that if I wanted to win. And I did that."
"I still don't have words to describe the win, I'm so incredibly happy," Guardini added.
It is the fifth victory for an Italian rider in this year's Giro, equalling the country's 1974 record.
Rodriguez maintained his 30-second lead over Victoria's Ryder Hesjedal in the overall standings after Hesjedal finished 67th.
The Spaniard will wear the pink jersey for an eighth day on Friday. He is 1:22 ahead of the two-time champion Ivan Basso.
"It wasn't so easy today because the speed we kept was very high, and I don't like this kind of pace," Rodriguez said. "Tomorrow and the day after we'll have a chance to gain some seconds over Hesjedal.
"It won't be easy because until now he has had a great Giro, but tomorrow's climbing is more suitable to my characteristics and I have to try to make him drop time," Rodriguez said.
Manuele Boaro, Stef Clement, Pier Paolo De Negri and Angelo Pagani broke away just over a kilometre into the stage. Their advantage quickly grew to over two minutes. However, the peloton kept them within range and caught up with the group at 66 kilometres to go.
Another break formed shortly after, but Mickael Delage, Martijn Keizer, Clement and Olivier Kaisen never gained substantial lead over the peloton.
Sky drove the peloton forward, keen to help Cavendish achieve a record fourth win in this year's Giro. With his main sprinting rivals pulling out ahead of the tough mountain stages, Cavendish was the favourite to win.
However, a powerful surge from Guardini in the final 500 yards saw him come up the outside to beat the world champion. Guardini's previous best finish in this year's race was 10th in stage 3.
Cavendish extended his lead in the points classification to 29 points ahead of Rodriguez and retains his grip on the red jersey. The Sky cyclist punched the air in an anger after crossing the line.
"I hate losing," Cavendish said. "It was a perfect sprint. Today he was fastest, he had more energy than me."
Carlos Ochoa and Martin Velits withdrew from the race overnight, leaving 167 riders still in the Giro.
Friday's 19th stage is the penultimate mountain ride, with five categorized climbs including two category one ascents along the 198-kilometre route from Treviso to Val di Fiemme.
The Giro ends on Sunday in Milan.Suggest a correction