The women’s 400 metres medley relay was first, and the American quartet delivered an emphatic victory in a time of 3:53.13.
Australia came up second with a time of 3:55.00 to take silver and Denmark rounded out the podium one-hundredth of a second later.
The men’s medley relay followed with its own moment of glory, maintaining an all-time perfect Olympic record in the event when the U.S. has participated.
Double gold medalist Ryan Murphy led off on backstroke, followed by Cody Miller on breaststroke, Phelps on butterfly and Nathan Adrian swimming the freestyle leg.
After Murphy delivered a world-record breaking backstroke leg, the writing was seemingly on the wall.
Phelps did his part in the third leg, pressing ahead of Great Britain and setting up Adrian to bring home the gold with an Olympic-record time of 3:27.95.
The Americans survived a stout challenge from the British, keyed by a lightning breaststroke leg by world-record holder Adam Peaty.
Australia took home bronze.
The relay event was the final competitive swim for Phelps.
He leaves the stage after five Olympic Games as by far the most decorated Olympian of all-time with 23 gold medals and 28 overall.
The 31-year-old Phelps furthered his legend by winning five gold and one silver at the Rio Games after coming out of retirement in 2014.
That gave the U.S. gold medals in five of six relay events, and 16 overall swimming golds.
It ties the American total from London in 2012, which was the most American gold medals in swimming since 1984, when the Eastern Bloc nations boycotted the Olympics.