Djokovic, who clinched the year-end number one ranking earlier in the week, was utterly relentless as he clinched a comprehensive bagel in the opening set with Querrey wilting in hapless fashion.
But the American, who fired down 18 aces in the two-hour match, responded doggedly in the second to level up the match after a tense tie-break, and produced an inspired performance in the third to cause a huge upset in the final Masters event of the year.
Djokovic looked alarmingly off colour after a convincing showing in the first set and Querrey, who secured arguably his greatest victory on the Tour, will next take on either 14th seed Milos Raonic, who came from a set down to beat home favourite Jeremy Chardy 6-7(4) 7-6(4) 6-3.
On his dreadful start, Querrey said: "I was embarrassed and wanted to walk off the court. But I finally held, then got a break, and I was on my way. I served terrific and I'm hoping to keep it up."
The 25-year-old Serb strode onto court wearing a Darth Vader mask to continue his tradition of marking Halloween in a comical manner, but the laughs were on Querrey in a desperately one-sided opening set.
The Las Vegas resident saw his serve ruthlessly taken apart in the opening game as Djokovic stamped his authority on proceedings, and it was one-way traffic from the outset.
Querrey quickly cut a despondent and dispirited figure on court as he slumped to 3-0 down without ever looking like holding serve or challenging his opponent’s delivery, and a third successive break of serve gave a scoreline that underlined this was barely even a contest.
The American spared himself the ignominy of a double bagel as he belatedly got on the board after slipping 2-0 down in the second set, and his first game was accompanied by a huge ovation as he punched the air in relief.
But the rather patronising applause was soon made to seem entirely out of place as Querrey reeled off three games in succession and even managed to move into the lead at 3-2 having stunned a suddenly sluggish Djokovic with a break of serve.
The second set continued to go to serve as Querrey, having eventually found his rhythm on the first delivery, traded regulation holds with his opponent, who began to appear frustrated and impatient at his resistance.
The set eventually drifted to a tie-break, and Djokovic immediately found himself behind with Querrey playing with real aggression to take it 7-5 and level up the match against the odds.
The third set was equally tight as Querrey was unerring on serve and potent in attacking his opponent’s delivery, and he broke in the fifth game with a stunning onslaught from the back of the court to leave Djokovic on the back foot.
It was a remarkable turnaround from the hopelessly one-sided opening set, and Querrey even managed to save three break-back points before holding under immense pressure to edge to within a game of victory.
The 25-year-old American held his nerve with three big first serves to close out the win under pressure as a desperate Djokovic was unable to redress the balance of the match, and Querrey again punched the air in delight.
Djokovic, who had compiled a circuit-best 70-11 match record going into the tournament and had been looking to reach his seventh Masters final of the season, looked drained as he congratulated Querrey, and will have been left shocked after losing a match he appeared to be in control of.
14-Milos Raonic (Canada) beat Jeremy Chardy (France) 6-7(4) 7-6(4) 6-3
Sam Querrey (US) beat 2-Novak Djokovic (Serbia) 0-6 7-6(5) 6-4
4-David Ferrer (Spain) beat Marcel Granollers (Spain) 6-1 6-3
8-Janko Tipsarevic (Serbia) beat Igor Sijsling (Netherlands) 6-4 7-6(0)
9-Juan Monaco (Argentina) beat Grigor Dimitrov (Bulgaria) 7-6(4) 6-2
Michael Llodra (France) beat 10-John Isner (US) 6-4 7-6(5)
11-Nicolas Almagro (Spain) beat Albert Ramos (Spain) 7-6(1) 6-7(4) 6-3
Kevin Anderson (South Africa) beat 12-Richard Gasquet (France) 7-6(5) 4-6 6-1
Gilles Simon (France) beat Victor Hanescu (Romania) 7-5 6-3
- Sports & Recreation