Federer, playing only his second match of a 2019 comeback to clay after three years away, needed two hours to defeat French showman Monfils 6-0, 4-6, 7-6 (7/3).
Osaka fell for the second time in two months to Federer's Swiss compatriot Belinda Bencic, exiting 3-6, 6-2, 7-5. Bencic also beat the double Grand Slam winner in March at Indian Wells.
Czech second seed Petra Kvitova, the defending champion, was also a shock loser, going down 6-2, 6-3 to Kiki Bertens, the Dutch seventh seed who was runner-up in the 2018 final.
Men's top seed Novak Djokovic, however, motored into the last eight as he hammered Jeremy Chardy 6-1, 7-6 (7/2) while five-time champion Rafael Nadal dispatched Frances Tiafoe 6-3, 6-4.
Nadal will next play Stan Wawrinka after the Swiss completed a national hat-trick on the day, beating Japan's sixth seed Kei Nishikori 6-3, 7-6 (7/3).
Defending champion Alexander Zverev came from breaks down in both of the last two sets to get past error-prone Pole Hubert Hurkacz 3-6, 6-4, 6-4 and now plays eighth seed Stefanos Tsitsipas, a 6-3, 6-4 winner against Fernando Verdasco.
Federer admitted he struggled to get past the talented Monfils, seeded 15th, before recording his 1,200th career win.
It took a comeback from 4-1 down in the final set after sweeping the first set in 18 minutes, combined with a dominating tiebreak for the Swiss fourth seed to advance.
Federer said nothing was decided until the end of a match played in difficult swirling winds and challenging conditions.
"I had a tough run of five losing games late in the second and into the third," said the 20-time Grand Slam champion who will face Austrian fifth seed and recent Barcelona champion Dominic Thiem for a place in the semi-finals. "I just hung in there, saving two match points (to force a final-set tiebreaker). It was a very stressful situation. Now that I'm through it, I'm very excited with the win.
"This match gave me a lot of information, it showed how tricky clay can be with the wind and everything. But I tried to take the bad bounces and the rest and just play positive tennis."
Against an inspired Bencic, Osaka served for the match in the third set leading 5-4, but could not touch a backhand return winner.
"When you play against a good player, you have to expect that they are going to play great at times," Osaka said.
COMMENTS
Comments are moderated and generally will be posted if they are on-topic and not abusive.
For more information, please see our Comments FAQ