The fourth seed dominated the first set but Azarenka fought back superbly and served for the match in the decider only for Williams to reel off four games in a row to win 6-2 2-6 7-5.
It was a fourth Flushing Meadows crown for the 30-year-old American, who looked amazed as she lay flat on her back on Arthur Ashe Stadium before going to celebrate with her friends and family.
"I honestly can't believe I won," she said at the post-match ceremony. "I really was preparing my runners-up speech. She's playing so great. I'm really shocked and so happy to have gotten so far."
Azarenka was gracious in defeat, and said she would leave Flushing Meadows with no regrets.
"At the moment it's tough, but Serena deserved to win," the 23-year-old said. "She showed how true a champion she is. I'm just honoured standing with such a champion here.
"I definitely gave it all today and stepping out of this court today I will have no regrets."
For Williams it was also redemption of a kind after last year's final defeat by Sam Stosur, which was marred by an altercation between Williams and umpire Eva Asderaki over a hindrance call, for which she was later fined.
Williams is only the world number four but she was a clear favourite going into the match after a stellar summer that included her fifth Wimbledon title and two Olympic gold medals.
The 30-year-old, meanwhile, had beaten Azarenka in nine of their 10 previous meetings and had dropped only 19 games on her way to the final.
Williams had not had the toughest path, playing only one player in the top 10, while Azarenka had had to battle her way past Stosur and Maria Sharapova, but the American looked in supreme form as she blasted her way through the first set.
But the tables turned completely in the second set as Williams began to make errors and Azarenka, who won her first grand slam title at the Australian Open as part of a brilliant opening to the season, capitalised.
Not since Steffi Graf beat Monica Seles in 1995 had there been a three-set women's final, and this one was now very much in the balance.
The intensity from both women was there for all to see and Azarenka, one of the sport's great fighters, did very well to stave off two break points in the second game.
She got her reward in the next game when Williams hooked a backhand just wide to hand the lead to her opponent for the first time in the match.
Azarenka had not lost a three-set match all year and she looked a lot more solid than Williams, whose unforced error count was well into the 30s.
But the American is also a natural-born battler and she broke back in a lengthy fourth game with two huge forehands, letting out a scream that was as long as it was loud.
Williams sent down one ace at 125mph but she always seemed liable to falter at any moment and it was Azarenka screaming when four errors from her opponent gave her the lead back at 4-3.
Williams smacked her racquet on the ground in frustration as an Azarenka shot hit the baseline to prevent the fourth seed breaking back and suddenly the Belarusian had a 5-3 lead.
But she could not serve it out, Williams breaking back to the delight of the American crowd and in particular her sister Venus watching in the stands.
The 30-year-old was moving her feet much better and the next time Azarenka served it was to stay in the match.
Williams was a woman on a mission and, although Azarenka came up with a superb volley to keep her at bay, it was only temporary, and she took her second match point when the world number one fired a backhand long.
COMMENT RULES: Comments that are judged to be defamatory, abusive or in bad taste are not acceptable and contributors who consistently fall below certain criteria will be permanently blacklisted. The moderator will not enter into debate with individual contributors and the moderator’s decision is final. It is Belfast Telegraph policy to close comments on court cases, tribunals and active legal investigations. We may also close comments on articles which are being targeted for abuse. Problems with commenting? email@example.com