Phelps, 31, secured his fifth gold of Rio 2016 when he teamed up with Ryan Murphy, Cody Miller and Nathan Adrian to set a new Games record of three minutes 27.95 seconds.
Britain finished 2.71 seconds adrift, with Australia in third.
USA also successfully defended their title in the women's 4x100m medley.
Kathleen Baker, Lilly King, Dana Vollmer and Simone Manuel clocked 3:53.13 to win USA's 1,000th gold at a summer Games.
"Getting off the bus and walking to the pool tonight, I pretty much felt myself starting to crack," said Phelps.
"Last warm-up, last time putting on a suit, last time walking out in front of people, representing my country... it's insane.
"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 American legend bowed out with 28 medals from five Olympics after making his debut in 2000.
That is 10 more than the next best. Gymnast Larisa Latynina, who competed for the Soviet Union between 1956 and 1964, won 18, nine of them gold.
"This all started with one little dream as a kid to change the sport of swimming and try to do something nobody has ever done," said Phelps.
"It turned out pretty cool. I've lived a dream come true. Being able to cap it off with these Games... it's just he perfect way to finish."
Britain's James Guy, who took on Phelps in the butterfly leg of the relay, said he expects to see the American racing at the Toyko 2020 Olympics.
"I don't think he'll retire," said Guy. "I think he wants 25 golds and he's got 23."
|1. Height is 6ft 4in but his disproportionate body has a torso as long as that of a 6ft 8in man and lower body of someone under 6ft - so he sits high in water and avoids drag
|2. Wingspan is usually same as height, but his wingspan is 6ft 7in - a huge three inches more than average
|3. Hyperextension of joints, including elbows, knees and chest, so he kicks from his chest down not ribs down like most others
|4. Phelps has been scientifically proven to produce less than half the lactic acid of his rivals - so he can recover quicker and win back-to-back golds on one evening
|5. He has double-jointed ankles that allow 15 degrees more flexibility than most swimmers - meaning his size 14 feet act like flippers
Murphy set the pace for the Americans in the first leg of the relay, recording a new backstroke world record of 51.85 seconds.
But a blistering breaststroke leg from 100m gold medallist and world record holder Adam Peaty, whose split was 56.59, put Britain into first place.
Phelps overtook Guy in the butterfly. Then Adrian held off Duncan Scott in the final leg to give the USA their 16th swimming gold medal in Rio.
Former British swimmer Karen Pickering on BBC One: "We've run out of words to describe what Michael Phelps has done.
"This comeback has coincided with such great form and we've seen much more of him emotionally. You get drawn into him.
"It seems such a shame if this is the last time we see him race."
USA team-mate Cody Miller tweeted: "The G.O.A.T Michael Phelps isn't just an incredible swimmer.. He's an incredible person and team-mate."
Phelps's partner, Nicole Johnson, tweeted: "I'm at a loss of words... Just so incredibly proud of Michael Phelps and the amazing week he's had!! What a way to close this chapter."
The White House's official Twitter account posted: "Hard work, focus, and a dream - that's the spirit of Michael Phelps. Tonight, we congratulate him and all of Team USA on making history."