The BMC rider and the peloton caught a breakaway with just a few hundred metres to go to the finish and got the better of Tinkoff-Saxo’s Sagan in a drag race. Third was Jan Bakelants (AG2R) who finished three seconds later.
The nature of the stage meant that the general classification was unchanged, with Chris Froome (Team Sky) and his rivals finishing safely just seven seconds after Van Avermaet.
The Tour de France peloton enjoyed a respite from the big mountains but the hard racing continued on the 198.5km transition stage from Muret to Rodez.
The course profile, with one intermediate sprint and three small category climbs with a punchy uphill finish suited to a certain kind of finisher, was one most experts earmarked for the points classification leader, Sagan. In the end it was a rider of similar talents who took the win.
Sagan conceded that he made a mistake on the final part of the 570-metre finishing climb of 9.7 per cent.
“I'm very angry, not disappointed. Very angry," Sagan said. "It was my mistake because I was waiting for too long and when we came near the top I was in the wheel of Van Avermaet but I should have continued to push out of the saddle.
“I could have won but I should have continued past him, when I came to his wheel.
“It was a very hard finish and I waited too long as I started a bit down but then I came to the wheel of Greg and I stayed there. I should have continued and in the final metres I didn’t have the power. I took points for the green jersey but I really want to win a stage for the team, my team-mates and myself.”
Van Avermaet said he was not aware that the rider chasing him was Sagan, who has now finished runner-up on four stages at this year's race.
"It was really close," Van Avermaet said of his victory. "I went really early because in Le Havre (Stage 6), everyone was waiting. So I tried to go from the bottom.
“It was really long the last 100 metres and I saw there was somebody in my wheel, so I just kept on sprinting. I was just hoping that he didn't come over me."
Australian Nathan Haas (Cannondale-Garmin) made the break of the day along with Alexandre Geniez (FDJ), Thomas De Gendt (Lotto-Soudal), Cyril Gautier (Europcar), Pierre-Luc Périchon (Bretagne-Séché Environnement) and Wilco Kelderman (LottoNL-Jumbo) but were kept in check by a peloton intent on a select sprint result.
The procession continued largely without a hiccup but with the time gap slowly edging downward. Haas launched a speculative attack with less than 20 kilometres to go but it was smartly shut down by his breakaway companions.
Five kilometres later Kelderman, Gautier and De Gendt dropped Haas, Geniez and Périchon and later held a 50-second lead on the peloton at 10km.
The trio drove hard to the finish in the desperate hope of beating the peloton only to be caught at the death.