Michael Phelps and the United States swim team closed out their Rio Olympics in the only way imaginable.

Gold.

Phelps put the United States ahead to stay on the butterfly leg of the men's 4x100 medley relay before Nathan Adrian finished the race off, giving the most decorated athlete in Olympic history his 23rd career gold medal.

If that was the end, and Phelps insists it is, what a way to go out.

He would finish his career with 28 medals overall, having won five golds and a silver at these games.

Two-time gold medalist Ryan Murphy put the Americans out front with a world-record split on the backstroke -- it counts since he was leading off -- but Britain surged ahead on the breaststroke with its own world-record holder, Adam Peaty. 

Phelps dove into the pool in second place. 

He wouldn't be for long.

On the return lap, Phelps powered through the water with his whirling butterfly stroke, surging ahead of James Guy to pass off a lead to the anchor Adrian.

It wasn't in doubt after that. Adrian pulled away on the freestyle to win in an Olympic-record time of 3 minutes, 27.95 seconds.

Kathleen Baker, Lilly King, Dana Vollmer and Simone Manuel led the American triumph. The winning time was 3:53.13.

Australia earned silver, while Denmark took bronze.

For Manuel, it was her second medal of the night -- she also won silver in the 50 free -- and second gold of the games. She became the first African-American woman to win an Olympic swimming title with her win in the 100 free.

Connor Jaeger gave the U.S. another silver in the 1,500-meter freestyle.

In all, American swimmers won 33 medals in the Rio pool, 16 of them gold. Australia was a distant second in the overall medal count, with 10. The Aussies and Hungary tied for the second-most swimming gold medals, with each nation winning three.

The 33 medals matches the U.S. total from the boycotted Los Angeles Games in 1984 and the Sydney Olympics in 2000.

The final two individual medals of the games went to Pernille Blume of Denmark in the 50 freestyle, her country's first swimming gold since 1948, and Italy's Gregorio Paltrinieri in the grueling 1,500 free.

After posting the top time in both the preliminaries and the semifinals, Blume came through again on the final night of swimming at the Rio Games. She finished in 24.07.

After her landmark victory in the 100 free, Manuel settled for silver this time in 24.09. Aliaksandra Herasimenia of Belarus earned the bronze in 24.11.

It was another huge disappointment for sisters Cate and Bronte Campbell of Australia. They were shut out of an individual medal again, with Cate finishing fifth and Bronte seventh.

Blume was the third Danish swimmer to capture a gold. Greta Andersen won the 100 free and Karen Margrethe Harup took the 100 backstroke at the 1948 London Olympics.

Paltrinieri pulled away from the field and was under world-record pace much of the race before fading a bit at the end. Still, he won comfortably in 14:34.57.

Jaeger claimed silver in 14:39.48, while bronze went to another Italian, Gabriele Detti, in 14:40.86.

Detti rallied over the final laps to pass American Jordan Wilimovksy, who settled for fourth.

Wilimovksy will get another shot at a medal in an even more demanding event -- the 10-kilometer open water race at Fort Copacabana on Tuesday.

The night, though, belonged to Phelps. For the last time. Phelps said he's all done.

In the only way imaginable.

The Associated Press contributed to this report.