Michael Phelps decided to give it one more shot, and as a result he cemented himself in the minds of a whole new generation of American sports fans.

The 31-year-old won a gold medal -- his fifth of the 2016 Rio Olympics and 23rd overall -- by swimming the butterfly in the 4x100-meter medley relay on Saturday night in the final race of his career.

The U.S. edged Great Britain and Australia in a time of 3:27.95, a new Olympic record, to take the gold. The U.S. has never lost the men's medley relay in Olympic history.

Ryan Murphy led off by swimming a world record in the backstroke, giving the U.S. a 1.7-second lead. Cody Miller did his job next with the breaststroke leg. Phelps went third, with Nathan Adrian finishing things up with his freestyle.

"We put together great relays," Phelps said after the race. "Murph starting off with a world record is just insane. ... I've had the opportunity to be on some of the greatest relays of all time. It truly is an honor."

Michael Phelps won gold on Saturday in the last race of his career. Getty Images

Leading up to and throughout the games, Phelps has continually reiterated that he will retire after his last race in Rio de Janeiro. If he does, he certainly will have gone out on top.

"I'm just ready for something different," Phelps continued. "My swimming career might be over, but I have the future ahead of me to turn the page and start whatever I want. It's not the end of a career, it's the beginning of a new journey."

At the Rio Games, Phelps won individual gold in the 200-meter individual medley and the 200-meter butterfly, while taking silver in the 100-meter butterfly. He also won team gold in the 4x100- and 4x200-meter freestyle relays, along with Saturday's 4x100-meter medley relay.

That's a total of five golds and a silver for the most decorated Olympian of all time.

In 2016, Phelps participated in his fifth Olympic Games. If he has indeed decided to retire, he will finish his historic career with 28 total medals, including 23 gold. Remarkably his first gold medal came in 2000 as a 15-year-old.