The 31-year-old third seed became the oldest US Open men's champion in 46 years when he 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.
"Novak, you're a great champion and a great person and because of you I'm where I am today. I'd like to congratulate you on everything you have done," said Wawrinka who has now won his last 11 finals.
"This is amazing. I came here without the goal of winning it but just to step on the court trying to win.
"My goal is to give everything I have to be the best I can."
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.
"Stan, you absolutely deserved to win. You were the more courageous player in the decisive moment and you deserve this title," said Djokovic.
"We both felt the demanding match that we played, but he came out on top, he was 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 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 apologized, 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.