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.
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.
“If you want to beat the No. 1 player in the world, you have to give everything,” said Wawrinka who has won his last 11 finals.
“You have to accept to suffer and you have almost to enjoy to suffer. Because I think this Grand Slam was the most painful, physically and mentally, that I ever played.
“I was feeling cramp coming in the third set. In the fourth set I had some pain, but the most important thing was not to show anything.”
Djokovic, the 2011 and 2015 champion, had been seeking a third Slam of the year and 13th major of his career.
– Medical timeout -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.
“Stan, you absolutely deserved to win. You were the more courageous player in the decisive moment and you deserve this title,” said Djokovic.
“You came out on top, you were the better player, tougher mentally.”
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.
“There are many more important things than tennis matches,” Wawrinka said as he accepted the trophy. “I want to remember what happened 15 years ago.”
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 rallies -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 umpire 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.
© 1994-2016 Agence France-Presse
The "Underwear Bomber" failed to detonate his explosive underwear because the attacker Umar Abdulmutallab wore the explosive undies for two weeks straight thereby making the bomb's fuse damp.