World number three Federer will unseat second-placed Nadal in the ATP rankings on the back of his incredible victory.
The Swiss player saved five match points, including three at a third set tie-break, before converting his first chance to seal a gripping 7-5, 3-6, 7-6 (9/7) victory against Leonardo Mayer.
The 17-times Grand Slam winner needed to draw on all his years of experience to remain level-headed as he closed out the game, leaving his Argentine opponent distraught as he trudged off the court.
Second seed Nadal, meanwhile, battled the early stages of appendicitis and a ruthless Feliciano Lopez in his 6-3, 7-6 (8/6) opening game loss.
The current world number two was diagnosed with appendicitis on Sunday but did not show any obvious signs of suffering from it in the match.
But he was still far from his world-beating best as he returns to competition following three months on the sidelines.
The Spaniard was competing in his second tournament since his exit from Wimbledon in July, after being dumped out of the China Open in the quarter-finals last week by a qualifier.
Nadal lost the first set, but looked to be proving the doubters wrong as he led 5-2 in the second. But Lopez, who is ranked 21st, powered back to force a tie-break and claimed victory with his first match point.
“I lost. Feli played better than me. That’s it,” Nadal said afterwards when asked about his condition.
“I talked about that yesterday, so I don’t need to talk again about my health.”
Elsewhere, Serbian world number one Djokovic claimed a 6-3, 6-4 victory against Austrian Dominic Thiem, the youngest player in the top 50.
The top seed in Shanghai broke his 21-year-old opponent in his final service game in the first set, and then again early in the second.
Thiem, who is ranked 39th, then forced two break points in Djokovic’s next two service games, but failed to capitalise, leaving his opponent to close in on victory in one hour 28 minutes.
But it was agony for Australian Open champion Stan Wawrinka, who crashed out of his second successive tournament at the first stage — despite leading his final set 3-0.
The fourth seed was in need of a morale-boosting victory after his last outing resulted in a first round exit at the Japan Open to 103-ranked Tatsuma Ito.
The world number four started brightly in Shanghai, taking the first set in his tightly-fought battle with Gilles Simon of France.
But his 29th-ranked opponent fought back to earn a 5-7, 7-5, 6-4 victory.
– ‘It’s never easy’ –
The results in Tokyo and Shanghai could undermine Wawrinka’s bid to book his place at the end of season ATP World Tour Finals, which features the world’s top eight players.
“Two first rounds. It’s never easy, for sure,” Wawrinka said.
“Two weeks. Didn’t win matches. But you have to accept that and see what was the problem.
“I need to accept, go back home, try to focus on the rest of the year because there are so many to play, so many big challenges for me, that I want to be fresh and ready mentally for that.”
The fifth seed in Shanghai, David Ferrer, also faced a tough battle in his opening match, but finally overcame Slovak Martin Klizan 4-6, 7-6 (7/4), 6-4.
Klizan dumped Nadal out of the China Open, but could not repeat his heroics against his Spanish opponent at the Masters 1000 event.
Ferrer will play Andy Murray in the next round, after the British 11th seed won his match against Poland’s Jerzy Janowicz 7-5, 6-2.
Meanwhile, seventh seed Kei Nishikori and 10th seed Grigor Dimitrov both crashed out in their opening matches.
The Japanese star looked injured during the closing stages of his 7-6 (7/5), 6-4 defeat to American Jack Sock, and received treatment with the scores at 4-3 in his opponent’s favour.
The US Open finalist won his next service game, but appeared slightly in pain as he lost the next two, which ended his participation in the tournament.
Bulgaria’s Dimitrov was dumped out by Julien Benneteau of France 7-5, 6-3.
Eighth seed Canadian Milos Raonic also failed to make the next round, after he withdrew from his match with Juan Monaco of Argentina suffering from fever as he was trailing 5-2.
The ATP World Tour Masters 1000 event concludes in Shanghai on Sunday.