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.
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 fight off two break points in the second game.
Watch: Serena wins US Open title... More Videos
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.