It was the first time since the Miami Masters in March 2010 that Djokovic has been eliminated so early in a tournament.
"During the second set I already felt that physically I'm down, and I struggled (in) every game,'' Djokovic said. "It's unfortunate, but on the brighter side, I have a little bit more time to rest because I had really difficult period in the last couple of weeks. Some things happened and a lot of things on my mind.''
The Serb entered the court wearing a Darth Vader mask on Halloween, and continued to put on a show by winning the first set in just 21 minutes.
"It was a little embarrassing,'' Querrey said. "But then I got rolling and got more confidence and started serving better and being a little more aggressive.''
Djokovic then started to waver under the relentless accuracy of the American's big serve, and made too many unforced errors the rest of the way.
Querrey hit 18 aces and converted his second match point when Djokovic's return sailed long.
Djokovic had already secured the year-end No. 1 ranking after defending champion Roger Federer pulled out of the tournament to rest for the season-ending ATP World Tour Finals in London.
After sealing the first set with some extravagant shot-making, everything had pointed to a comfortable win for Djokovic after he broke for a 2-0 lead in the second set. But Querrey found his range, hitting 10 aces during the set. Djokovic played too many loose shots in the tiebreak and Querrey evened the match on his first set point when Djokovic's forehand clipped the net and bounced wide.
"I was concerned about how long I can keep that level, since physically I'm not feeling very good in last couple of days,'' Djokovic said. "When you're playing somebody that hits, that serves that well in the corners, there is nothing you can do.''
The match turned in Querrey's favor in the fifth game of the deciding set, when he broke for a 3-2 lead with a stinging forehand winner, and held for 4-2 after another sloppy forehand from Djokovic landed in the net.
Djokovic battled back and had a great chance to even the set at 4-4, but Querrey saved five break points in the eighth game, three of them aces.
"I thought I served amazing, especially the big points. I felt like I made a first serve there every time,'' Querrey said. "I could see he was struggling a little bit, missing some shots he probably wouldn't normally miss.''
Querrey will play 14th-seeded Milos Raonic of Canada in the third round. or Frenchman Jeremy Chardy in the third round. Raonic had 26 aces in a 6-7 (4), 7-6 (4), 6-3 win against Jeremy Chardy of France.
Murray, fourth-seeded David Ferrer of Spain and No. 7 Juan Martin del Potro of Argentina won their matches comfortably.
Murray saved two break points before clinching the first set when France's Paul-Henri Mathieu returned his looping second serve into the net. The Briton sealed the win on a double-fault from Mathieu. He next plays Jerzy Janowicz of Poland.
"I played him in Davis Cup a few years ago. He's a huge guy, big serve. He's having his best year on the tour,'' Murray said. "It will be a tough match. I mean on courts like this, if he serves big he's going to be a tough guy to break.''
Del Potro served six aces and broke Alejandro Falla of Colombia three times in each set to win 6-2, 6-2. He next faces Michael Llodra of France.
Ferrer beat Marcel Granollers 6-1, 6-3 in an all-Spanish match. Ferrer, who is tied with Roger Federer for the most tour titles this year with six, faces 16th-seeded Stanislas Wawrinka of Switzerland in the third round.
Eighth-seeded Janko Tipsarevic of Serbia also advanced to the third round, beating Dutchman Igor Sijsling 6-4, 7-6 (0), and keeping alive his slim chances of qualifying for the ATP Finals.
Ninth-seeded Juan Monaco of Argentina beat Bulgarian Grigor Dimitrov 7-6 (4), 6-2 and still has an outside chance of reaching London. Tipsarevic and Monaco play each other next, but No. 12 Richard Gasquet's chances of making it to London have gone after a 7-6 (5), 4-6, 6-1 loss to South African Kevin Anderson.
There were also wins for 11th-seeded Nicolas Almagro of Spain, Llodra and Gilles Simon of France.
Almagro beat countryman Albert Ramos 7-6 (1), 6-7 (4), 6-3; Llodra downed No. 10 John Isner of the United States 6-4, 7-6 (5), and Simon won 7-5, 6-3 against Victor Hanescu of Romania.
Rafael Nadal, who is working his way back from a knee injury, also skipped the tournament.