Phelps finished with a time of 1:54.27, 0.63 seconds faster than Lochte.
It was one of the few times in his Olympic career that Phelps was the underdog. Lochte was the favorite after having the best time in Wednesday's semifinals. He also defeated Phelps in the 400m individual medley.
It's the fourth medal four Phelps at the 2012 Olympics and his second gold in London.
The two medals help the USA stay ahead of China in the overall medal count. Heading into the race, the U.S. had 35 total medals to China's. The win now puts the two nations in a tie with 18 gold medals apiece.