Wawrinka, seeded third, won 6-7 (1-7) 6-4 7-5 6-3 to add the US title to his 2014 Australia Open title and his French Open triumph last year.
The 31-year-old hit 46 winners in a brilliant display of shot-making.
Serbia's Djokovic made a strong start but struggled physically as the final entered a fourth hour in New York.
- US Open men's final as it happened
- Kerber beats Pliskova in women's final
- Jamie Murray & Soares win doubles title
Wawrinka has now won all three Grand Slam finals he has played and his last 11 finals in a row, and has become the oldest male winner of the US Open since 35-year-old Ken Rosewall in 1970.
"My goal is to give everything I have to be the best I can," said Wawrinka, who saved a match point in the third round against Britain's Dan Evans.
"I never had a goal to win a Grand Slam but I am trying as best I can. Match after match I was playing better."
Djokovic, 29, was the defending champion and trying to win his third Grand Slam title of 2016 and 13th of his career
He required treatment for a blister on his big toe in the final set, prompting complaints to the umpire and tournament referee from Wawrinka as the interruption came before his service game.
"Stan, sorry! I can't stand," Djokovic shouted across to him during the break.
After the match, Djokovic said: "Congratulations, Stan, to your team as well. This has been absolutely deserved today. You were the more courageous player in the decisive moment and he deserves his title."
'I came here without the goal of winning'
Wawrinka had won just four of 23 previous matches against Djokovic but two of those had come on the way to winning Grand Slam titles, and the contest brought out the best in him once again.
Djokovic had spent just nine hours on court getting to the final, compared to 18 for Wawrinka, and the Serb started much the sharper.
However, a comfortable 5-2 lead disappeared as Wawrinka began to find his range and it required a surge of adrenaline and form to take the Serb through the tie-break.
The Swiss claimed his only point of the tie-break in an astonishing rally of attack and counter-attack, and his groundstrokes began to flow as he took control of the final in the second set.
Djokovic pegged back an early break but Wawrinka continued to exert pressure, setting up two set points with a forehand winner and celebrating as the Serb went wide, smashing his racquet in frustration.
There were early signs of some physical discomfort when Djokovic patted over two smashes at 3-0 down in the third set but again he broke back, converting just his third break point of 14.
Another tie-break loomed but the sheer weight of shot coming at him across the net drew a 14th error of the set to edge Wawrinka ahead.
The Swiss looked much the stronger in the fourth set, and doubts only emerged when Djokovic called for the trainer to treat a blister - something that riled Wawrinka as it occurred ahead of his service game.
Wawrinka appeared to have been distracted as he fell break-point down three times in the following game, but he held on each time, pumping his fist as he clinched what proved to be the decisive hold.
Djokovic continued to chase down as much as he could, despite a bloodied toe, but Wawrinka converted his second match point to claim his third major title in as many years.
"This is amazing," he added. "I came here without the goal of winning it but stepped on the court trying to win the match.
"I played a lot of tennis, I am completely empty. There was so much emotion with the crowd, the atmosphere, the stadium, it's been an amazing night."
Djokovic had arrived in New York with doubts around his state of fitness following a wrist injury, his form after defeat at Wimbledon and the Olympics, and the added concern of unspecified "personal issues".
The Serb, 29, was helped in terms of recovery by two retirements and one walkover among his six opponents on the way to the final, but he still came up short physically.
"At this level, after playing seven, eight months of the season, obviously you're not very fresh but coming into the Grand Slam final you are giving it your best," he said.
"We both felt it. We both felt the demanding match that we played today, but he came out on top, he was the better player, tougher mentally.
"It was a fantastic couple of weeks for me. I didn't know if I was going to come a few weeks before because I struggled physically. If someone had told me I was going to play in the final I would definitely have accepted it."