Monfils went toe-to-toe with the 17-time Grand Slam champion during a feast of attacking tennis, eventually emerging on top 6-4, 6-7 (5/7), 6-3. (Agencies)
Federer, playing his first tournament since the US Open, started in the worst possible way, limply losing his first service game to immediately hand the initiative to Monfils, who went on to take the opener.
Despite vocal support from a partisan crowd, Federer was broken midway through the second set but he broke back, sealing the game with an overhead smash.
Monfils, clad in bright orange, drew first blood in the tie-break but crucially put a relatively straightforward forehand long when he had a chance to engineer match points, allowing Federer back into the set and the Swiss made no mistake.
With both players sticking to a plan of all-out attack, Monfils grabbed the initiative, breaking to take a 3-1 lead and managed to save the break points that Federer carved out before going on to seal the match.
Federer, who made early exits at Wimbledon and the US Open, came to Shanghai seeking much-needed points as he bids to reach next month's World Tour Finals, which he has won a record six times.
Earlier, seventh seed Jo-Wilfried Tsonga brushed aside Kei Nishikori to reach the quarter-finals as Juan Martin del Potro progressed with a walkover.
The powerful Tsonga had a battle on his hands early on against the Japanese 12th seed but came out on top in a first-set tie-break and romped through the second set without losing a single game to record a comfortable 7-6 (7/5), 6-0 victory.
Monfils went toe-to-toe with the 17-time Grand Slam champion during a feast of attacking tennis, eventually emerging on top 6-4, 6-7 (5/7), 6-3.