Wawrinka, the 31-year-old third seed, triumphed 6-7 (1/7), 6-4, 7-5, 6-3 after almost four hours of high intensity tennis.
READ MORE: Tennis: Biggest upset of the Wimbledon
He even held his nerve when the final threatened to erupt into controversy after Djokovic twice called medical timeouts in the fourth set.
Wawrinka is the oldest winner of the men's tournament since 35-year-old Ken Rosewall in 1970 and the first over 30 to take the title since Pete Sampras in 2002.
It was the Swiss player's first New York title, following his breakthrough 2014 Australian Open triumph and 2015 Roland Garros crown.
On both those occasions, Djokovic had been the fall-guy, losing in Melbourne in the quarter-finals and ending as runner-up in Paris.
Djokovic, the 2011 and 2015 champion, had been seeking a third Slam of the year and 13th major of his career.
But the 29-year-old Serb finished runner-up in New York for the fourth time after paying a heavy price for converting just three of 17 break points.
Wawrinka, who had to save a match point to beat unheralded Dan Evans in the third round, committed 51 unforced errors but crucially smashed 46 winners past his opponent.
The final started against a sombre but defiant background with Broadway star Norm Lewis delivering a stirring rendition of The Star Spangled Banner, a poignant performance on the 15th anniversary of the 9/11 attacks.
Djokovic, playing in his 21st Grand Slam final and seventh in New York, was quickest out of the blocks.
He broke for 2-0 having won the first point of the game with two extreme, against-the-odds retrievals.
Trailing 2-5, Wawrinka, who had spent the best part of nine hours more than Djokovic getting to the final, saved two set points.
The Serb was made to pay for his wastefulness as the Swiss suddenly found his range, breaking back in the ninth game off a Djokovic double fault.
But Djokovic glided untroubled through the tie-break as Wawrinka's serve fell apart.
Wawrinka is used to chasing lost causes against Djokovic, coming back from losing the first set to win their 2014 Australian Open quarter-final and again at Roland Garros last year.
Calling on that expertise, he broke for 3-1 in the second set, recovered from handing the advantage back in the seventh to level the final when Djokovic unleashed a wild forehand on set point in the 10th.
Frustrated, Djokovic demolished his racquet, collecting a code violation from mpire Ali Nili.
Wawrinka saved three break points in the opening game of the third set, which featured a 28-shot exchange, broke in the second and saved another break point in the next for a 3-0 lead.
But at the sixth time of asking in the set, Djokovic clawed his way back with a break for 2-3.
Wawrinka, however, kept up the attack and on set point in the 12th game slotted a backhand deep and into the corner which Djokovic could only push wide.
The world number one looked spent. He appeared to start cramping in the fourth set as he quickly slipped 0-3 down.
Much to Wawrinka's irritation, at 1-3 Djokovic called the trainer and took a medical timeout for treatment for blisters.
Djokovic apologised, claiming he needed attention as he couldn't stand.
At 2-5, the trainer reappeared but Wawrinka saw the job through leaving Djokovic to finish a Slam runner-up for the ninth time.