He and his American teammates closed out a dominating weeklong performance during which the United States won 30 swimming medals, 16 of them gold.
Phelps teamed with Matthew Grevers, Brendan Hansen and Nathan Adrian to win the 400-meter medley relay. Phelps, taking his turn on the butterfly leg as the third swimmer in the water for his team, began his 100-meter trip behind a Japanese rival and finished it in front.
Adrian did the rest, making sure Phelps went out a winner.
The 18 gold medals for Phelps while he swam in the Sydney, Athens, Beijing and London Olympics are twice as many as any other athlete has won in the history of the Games. His total of 22 medals is the record by four.
The American women also won their medley relay and had they not done so it would have been a massive shock.
Each of the U.S. swimmers -- Missy Franklin, Rebecca Soni, Dana Vollmer and Allison Schmitt -- won gold in their individual specialty during the Olympics and when they joined forces they broke the world record with a time of 3:52.05.
Among them, those four American women collected 12 gold medals in London.
The other two finals on the last day of Olympic swimming were won by Ranomi Kromowidjojo of the Netherlands in the women's 50-meter freestyle and China's Sun Yang in the men's 1,500-meter freestyle.
Sun, world record holder and overwhelming favorite in the long-distance race, survived potential disqualification when he dove into the water before the starting tone rang out.
Judges ruled that a sound in the swimming arena could have distracted him and he was allowed to climb back onto the starting block.
He then lowered his own world mark by more than 2 seconds while swimming a time of 14:31.02.
Oussama Mellouli of Tunisia, who won the race at the 2008 Olympics, finished second this time.
Kromowidjojo added the 50-meter gold medal to the one she won in the 100. Aliaksandra Herasimenia of Belarus finished second in both races.
Australia and Japan switched places in the two relays -- the Australians finishing second in the women's event and third in the men's. Japan was second in the men's and third in the women's.
The Japanese had an interesting swimming experience during the Olympics. They didn't win a single race but they finished second on three occasions and they wound up with eight bronze medals.
Swim Week Miami Beach 2014 [PHOTOS]