Belgian rider Jan Bakelants did enough in the sweltering heat to keep the race leader’s yellow jersey.
Gerrans looked to have the finish line in sight with about 100 meters to go, though the Slovakian rider put on a late sprint and almost caught him.
But Gerrans dug deep to clinch his second career Tour stage win. Spaniard Jose Joaquin Rojas finished third.
“To beat a guy like Sagan in a sprint like that, I’m very happy,” the 33-year-old Gerrans said. “It went perfectly for me.”
Gerrans was slowing up, but just managed one last effort to throw his bike forward the way a 100-meter runner would dip for the line.
“I wasn’t sure if I had won – a half-wheel length?!” Gerrans said. “All went perfectly well, my team took great care of me after the last climb.”
He will also need to thank his teammate Simon Clarke, who placed himself in the early breakaway.
“It was the team plan. I was brought to the Tour de France to join breakaways, so I made sure I did my job,” Clarke said. “I was quite relaxed today and when you’re relaxed it means you have good legs.”
It was a particularly welcome win for Gerrans’ Orica Greenedge team after the confusion of Saturday’s first stage, when the team bus was stuck on the finish line and removed moments before the riders arrived.
“We saw the footage,” Gerrans said. “You really can’t do (anything) but laugh at the situation.”
Sagan is in the coveted sprinter’s green jersey he is expected to contest with British sprinter Mark Cavendish.
Bakelants, the winner of Sunday’s second stage, finished in 19th place.
“The team worked very hard for me, and I’m very happy to keep the yellow jersey,” Bakelants said. “It was a very hot day and the conditions were not easy.”
Monday’s 90-mile trek started from Ajaccio, where French emperor and military mastermind Napoleon Bonaparte was born in 1769, and finished in Calvi after three moderate climbs and a steeper last climb tested the legs of the peloton.
Gerrans clocked a winning time of 3 hours, 41 minutes, 24 seconds.
It was the last of the trio of Corsican stages before the race heads back to mainland France for Tuesday’s team time trial in Nice.
A five-man breakaway consisting of Frenchmen Cyril Gautier, Alexis Vuillermoz, Sebastien Minard, Clarke and Dutchman Lieuwe Westra was gradually pegged back as the stage snaked through the Corsican hills.
Nearing the halfway point, riders cut through the sleepy village of Piana, and then sped down the narrow roads along canyons overlooking the simmering blue waters of the Mediterranean.
With the Tour heading through Corsica for the first time, some fans got their first glimpse of the showcase race – and made a point of getting noticed.
One defied the sweltering heat to dress up in a full Napoleon outfit, saluting from the roadside.
Further on, a man held up a Coriscan flag as he rode on horseback alongside the rolling pack.
“The scenery was great, but for us it was super stressful,” veteran French rider Sylvain Chavanel said.
Clarke was the last of the breakaway group to be caught on the final climb – the day’s hardest, a 2-mile ascent up Col de Marsolino with a sharp gradient of 8.1 percent.
Frenchman Pierre Rolland dominated the climb and was about 10 seconds clear when he went over the top. But he was caught on the descent by Chavanel, Norwegian Lars-Peter Nordhaug and Spaniard Mikel Nieve.
“It’s hard to hold on when you’re on your own,” said Rolland, who kept hold of the polka-dot best climbers’ jersey.