World number three Federer will unseat second-placed Nadal in the ATP rankings on the back of his incredible victory.
The Basel native 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-time 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 quarterfinals 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.
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 capitalize, 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.