RIO DE JANEIRO-Michael Phelps brought the curtain down on one of sport's most storied careers with a dynamite relay swim to give himself 23 Olympic gold medals and declared it a perfect finale.

The American, far and away the most decorated athlete in Olympic history, finished with five golds and a silver in Rio after signing off in dramatic style, coming to the rescue in the 4x100m medley final. Turning back the clock in his fifth and final Games, Phelps produced a blistering third leg of butterfly to restore his team's lead and tee up victory after Britain's Adam Peaty had threatened to gatecrash his party.

"Getting off the bus and walking to the pool tonight, I pretty much felt myself starting to crack," confessed Phelps. "Last warm-up, last time putting on a suit, last time walking out in front of people, representing my country -- it's insane, a lot better than it was four years ago," added the 31-year-old, who retired after the 2012 London Games before returning for one last hurrah.

"This is how I wanted to finish my career. I've lived a dream come true. Being able to cap it off with these Games is just the perfect way to finish."

The hullabaloo over the Phelps's farewell overshadowed the achievement of the women's 4x100m medley relayers as they captured their country's 1,000th Olympic medal, according to the United States Olympic committee. The country's first medal dates back to 1896, when James Connolly won triple jump gold.

Even that statistic seemed to pale against the irrepressible Phelps -- and likewise Ryan Murphy's leadoff backstroke leg, which set a new 100m world record of 51.85. "No matter what country you swim for you're indebted to Michael Phelps," said Murphy after winning his third Rio gold. "He's opened a lot of doors for all of us. It was awesome to be in that race with him."

Nathan Adrian brought the Americans home to preserve their unbeaten Olympic record in the event and give them a 16th swimming gold from 32 events for the week, matching their haul in London four years ago. An Olympic record time of 3:27.95 was lost in the emotion after the race as Phelps tearfully saluted the crowd while his beauty queen fiancee Nicole Johnson looked on sobbing as she cradled baby son Boomer.

Britain took silver, thanks largely to a breaststroke leg from Peaty that was quicker than the world record he set to win individual gold, but won't count as it came in the middle of a relay.

Australia claimed bronze, but the memory of sharing the pool with Phelps in his final race could last longer than the glow of their relay gongs. The women's quartet of Kathleen Baker, Lilly King, Simone Manuel and Dana Vollmer made history, but were made to work.

The Americans, who failed to medal at last year's world championships, had looked in peril when two-time doping violator Yulia Efimova gave Russia the lead on the breaststroke.

But Vollmer's butterfly averted a crisis and Manuel brought them home in 3:53.13 almost two seconds ahead of Australia with Denmark taking bronze. "A 1,000th gold for team USA is a nice number," said Manuel. "It's really special. Just sharing that with three other women, it's the icing on top of the cake." Pernille Blume, who swam Denmark's anchor leg, earlier won gold in the women's 50m free, inflicting more heartbreak on Australia's Cate Campbell after her 100m flop.

Blume won in 24.07 with Manuel adding silver to her surprise 100m gold with a time of 24.09 and Aliaksandra Herasimenia of Belarus, the London runner-up previously banned for doping, taking bronze. "It's possibly the greatest choke in Olympic history," said 100m world record holder Cate, who finished fifth. "It hurts. I almost needed someone to give me a Heimlich manoeuvre a couple of nights ago."

Campbell returned around 20 minutes later for the 4x100 medley relay but despite a fine anchor leg could not prevent the United States retaining their Olympic title. World champion Gregorio Paltrinieri blazed to gold in the men's 1,500m in the absence of defending champion Sun Yang, who crushed out in the heats blaming a cold, the Italian smashing a quality field to win in 14:34.57.



great Michael Phelps moments

Race of the century

JIt was swimming's race of the century: 19-year-old emerging star Phelps up against the great Ian Thorpe and compatriot Grant Hackett and defending Olympic champion Pieter van den Hoogenband in the 200m freestyle in Athens. Phelps set an American record but finished third. The defeat only fueled Phelps -- who set a world record in a dominant 200m freestyle triumph four years later in Beijing.



Narrowest win

Phelps' seventh gold medal in Beijing came by the narrowest of margins, in a scintillating duel with Milorad Cavic -- the tough-talking Serbian who said he'd like to go down in history as "some guy" who spoiled Phelps's eight-gold bid. In the lone final in which he failed to set a world record, Phelps beat Cavic by one one-hundredth of a second. In Athens, he beat world record-holding teammate Ian Crocker by four one-hundredths of a second.



Avenging defeat

Phelps wrested back the 200m butterfly crown in Rio -- avenging his defeat to le Clos who faded to finish fifth and returning to the top in the event that started it all. The win made Phelps the oldest individual swimming gold medallist in Olympic history at 31. There to share it were fiancee Nicole Johnson and three-month-old son Boomer, whose kiss from his teary dad melted hearts at the Olympic Aquatics Stadium.