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.
On both those occasions, Djokovic had been the fall-guy, losing in Melbourne in the quarter-finals and ending as runner-up in Paris.
More From This Section
"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."
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."
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.
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.
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.