In the first three-set final at the US Open since 1995, American fourth seed Williams joined her sister Venus and Steffi Graf as the only women to win Wimbledon, the Olympics and the US Open in the same year.
Pushed to her limit for the only time in the Flushing Meadows fortnight, Williams was down a break twice in the third set but battled back to win after two hours and 18 minutes when Azarenka swatted a backhand long.
"I honestly can't believe I won," Williams said. "I was preparing my runner-up speech. I'm so shocked. It's remarkable."
Williams, who turns 31 on September 26, became the second-oldest US Open women's champion in the Open era. Australian Margaret Court was one month past her 31st birthday when she won the 1973 title.
Not since Martina Navratilova in 1987 had a woman won the US Open title past her 30th birthday.
Taking home a top prize of $1.9 million, Williams won her 45th career title after finishing second to Australia's Samantha Stosur in 2011 following a year of health problems that included blood clots on her lungs.
Williams won prior US Open titles in 1999, 2002 and 2008 and added Grand Slam crowns at the 2002 French Open, the 2003, 2005, 2007, 2009 and 2010 Australian Opens and Wimbledon in 2002, 2003, 2009, 2010 and 2012.
Olympic bronze medalist Azaranka, whose 1-9 record against Williams in prior matches included losses in this year's Olympic and Wimbledon semi-finals, won her first Grand Slam title at this year's Australian Open.
But no women's top seed has won a US Open title since Justine Henin in 2007 and none has won a Grand Slam title since Serena Williams in 2010 at Wimbledon.
"Serena deserves to win. She showed how true a champion she is," Azarenka said. "I definitely gave it all today. Stepping off this court I will have no regrets."
Williams, in a third set at the US Open for the first time since 2006, exchanged early breaks with Azarenka in the final set.
Azarenka, unbeaten in 12 prior three-set matches this year, broke Williams at love for a 4-3 lead, but was broken back when she served for the match in the 10th game, netting a forehand to pull Williams level at 5-5.
The American held serve and then watched as Azarenka twice hit errant shots when she was a point from forcing the first third-set tie-breaker in a US Open women's final since 1985. Williams then won on her first match-point chance.
The 13-year gap between Williams winning her first Slam title at age 17 at the US Open and now marks the longest span between first and most recent titles in US Open history and for any Grand Slam in the Open era that began in 1968.
No US Open women's final had gone to a third set since Steffi Graf went the distance to defeat Monica Seles 7-6 (8/6), 0-6, 6-3 some 17 years ago.
Williams stretched her match-win streak against World No. 1 players to five in a row, last losing to a rankings leader in 2007 when Justine Henin beat her, and 15 for her career, three shy of Navratilova's all-time record.
It was the fourth time Williams defeated a World No. 1 in a Grand Slam final after Martina Hingis in the 1999 US Open, Venus Williams in 2002 Wimbledon and Lindsay Davenport in the 2005 Australian Open.