Five-time Grand Slam winner Djokovic looked to be heading for a routine victory when he won the opening set 6-0 against his American opponent with just 20 minutes of the match gone.
However, Querrey responded in the second set by holding all of his service games. Djokovic followed suit and as a result a tie-break was required to determine a winner.
It was Querrey who claimed the early break and the world number 22 held his composure to force a deciding set thanks to a 7-5 success.
Twenty-five-year-old Querrey then took control of the third set when he broke for a 2-1 lead.
Djokovic had the chance to break back in the eighth game of the set, but the Serbian spurned three opportunities as Querrey was eventually able to hold for a 5-3 lead.
Another hold from Djokovic followed, before Querrey was able to serve out for his first victory over one of the world's top three players.