The world number three produced an opening set of flawless tennis before losing his way in set two.
Ranked 86 places below Federer and playing on his least favourite surface, Mahut capitalised on an unusually error-prone display from the Swiss to convert the set's only break point and level the match.
Federer quickly rediscovered his best form though and wrapped up the 6-3 4-6 6-2 7-5 victory in two hours and 37 minutes.
Mahut, who is known best for his dominating serve, failed to record a single ace in the final two sets.
If Federer overcomes David Goffin in the next round he will have reached the quarter-finals of 32 consecutive Grand Slams.