World number one Azarenka was serving for the title at 5-4 in front in the deciding third set.
However, Williams responded to win three games in succession to claim her fourth Grand Slam title at Flushing Meadows.
"I cannot believe I won," she said. "I was preparing my runner's-up speech because she was playing so great. I'm shocked."
Williams has now won 15 singles Grand Slam titles.