(CNN) -- Michael Phelps rounded off his record breaking career with his 18th Olympic gold medal as part of the winning U.S. 4x100m medley relay squad Saturday.
It was his fourth gold in London -- two individual, two team -- and extended his all-time Games record to 22 medals.
Phelps swam the third butterfly leg and helped overhaul surprise leaders Japan to give Nathan Adrian a small lead for the freestyle finish.
He had started his final Games with a disappointing fourth in the men's 400m individual medley behind teammate Ryan Lochte and was pipped in his trademark 200m butterfly by South African Chad le Clos.
But ever resourceful, Phelps, who won eight gold medals in Beijing in 2008, ended the meet as the leading swimmer with four golds and two silvers.
Fellow American 17-year-old Missy Franklin ended as the top female swimmer in terms of medal tally in London as she joined Allison Schmitt, Rebecca Soni and Dana Vollmer to win their medley relay in a world record time of three minutes 52.05 seconds.
Franklin also broke the world record in winning the 200m backstroke, one of her four gold medals, backed up by a bronze.
The most emphatic victory on the final night of swimming finals came courtesy of China's Sun Yang in the men's 1500m, the longest race in the pool.
A shout from a spectator distracted Sun and he false started, but was quickly reinstated in the race.
Once underway, it was a procession as he added to gold in the 400m freestyle by smashing his own world record with a time of 14 minutes 31.02 seconds.
"I thought I was going to be disqualified," Sun told gathered reporters. "I didn't expect the false start and I was very worried about being disqualified."
Sun ended with two golds -- the first man from China to win an Olympic swimming title -- and a silver from the 200 free.
Canadian Ryan Cochrane was more than eight seconds back with Beijing champion Oussama Mellouli in third.
The other gold of the night went to Ranomi Kromowidjojo, who took the women's 50m free from Aliaksandra Herasimenia of Belarus and Dutch teammate Marleen Veldhuis.
It completed the 50-100 double for Kromowidjojo.