RIO DE JANEIRO — As he walked into an Olympic pool venue for the final time on Saturday night, Michael Phelps couldn’t help but feel the emotion begin to settle over him. He had known this day had been coming for years. Ever since he decided to unretire for one last Olympics, he knew it would all come to an end late on Aug. 13.

He expected there would be emotions. How could there not? This was nothing like four years earlier in London, where he left the sport of swimming kicking and screaming and eager to put lane lines and kickboards in his rearview mirror as quickly as he could. This time, he had embraced the journey, became a team leader and rediscovered the love for the water he had as a little boy. So of course there would be tears, even before his final race began.

“Walking down the warm-up pool deck, I started getting choked up thinking this is my last ‘this,’ this is my last ‘that,'” Phelps said. “It was definitely more emotion than in 2012, and I think that’s a good thing.”

He wasn’t alone. Bob Bowman had coached Phelps for 20 years. He watched Phelps grow from an eager 11-year-old into a hard-headed, complicated competitor into a grown father and leader. He had once worried about who Phelps was hanging out with and what they might be up to. At one point, the relationship between Bowman and Phelps had devolved into ferocious, profanity-laced arguments, with both of them swearing they would never talk to the other again, only to somehow return.

And yet, here at the end, for the past 21 months, their bond had become everything it always should have been. They respected one another. Vacationed together. Laughed. Worked hard but had fun doing so. So of course, when Phelps began his final warm-up swim before the men’s 4×100 meter medley relay final, Bowman had to excuse himself.

“I just walked off like I was going to the bathroom,” Bowman said. “I spent the whole day thinking about sentimental stuff. The last time I will see him swim freestyle. The last 200 IM. The last time I’m going to watch him do this 25 or that 25. I got emotional. I don’t think he even knew.”

By the time Phelps stepped onto the competition deck, the tears were in check for the time being. He had just one thought: Get the lead. The United States had never lost the men’s medley relay, and they weren’t going to start now. Not in the last swim of Michael Phelps’ career. His motivational speech to his teammates was simple but effective.

“He said, ‘I don’t have anything to say, but let’s just go out there and kill it,'” Ryan Murphy said. “It wasn’t much, but that was enough for me to get hyped.”

Murphy began the American charge by setting a world-record time in the 100 backstroke during the first leg of the relay. But thanks to a monster breaststroke by Great Britain’s Adam Peaty, Phelps entered the water on the third leg with the Americans trailing by .61 seconds. In a way, this was exactly how it was supposed to be — the United States needing Michael Phelps to help them to victory.

One hundred meters and a little more than 50 seconds later, Phelps and his strong butterfly stroke handed teammate Nathan Adrian a .41-second lead that the Americans did not lose. And just like that, the greatest career in Olympic history came to an end.

“This was the cherry on top that I wanted,” Phelps said after the race. “I couldn’t be any happier with the end of my career.”

With that last race of the aquatics schedule in Rio, Phelps won his 28th career Olympic medal and his 23rd gold. Last week, he regained the 200 butterfly crown he had lost four years ago that had meant so much to him. He again beat teammate Ryan Lochte in the 200 individual medley for the fourth straight Olympics. And in the last individual event of his career, he took home silver in the 100 butterfly behind 21-year-old Joseph Schooling, who grew up in Singapore idolizing Phelps; it reminded the world just how hard greatness can be to master.

Phelps leaves Rio with five gold medals and one silver, the fourth straight Olympics where Phelps is likely to win more medals than any other Olympian.

When the race finally was over, Phelps raised his arms high above his head to acknowledge the cheering crowd. The emotions returned while he waited for the medal ceremony, but it happened behind closed doors, where no one could see it. Teammate Cody Miller became dizzy and light-headed, postponing the medal ceremony by some 25 minutes. By the time Phelps re-emerged, he put himself back together.

“You could see the tears in his eyes as we were walking to the medal ceremony,” Murphy said. “It got tampered down with how much time we had to wait, but he was super emotional. It was a great way to cap off an incredible career.”

For Phelps, these Olympics have proved to be different than any other. He walked in his first Opening Ceremony after being elected the U.S. flag-bearer by his teammates. He served as one of the team captains — another first — and help lead the Americans to 33 total swimming medals, the most since 1984.

And, for the first time in his life, it wasn’t all about winning. It was the process of getting there. Which is why those close to Phelps are optimistic that the next chapter of his life will be far brighter than it might have once looked. For years, Phelps defined himself as nothing more than a swimmer. In 2008, when a University of Michigan athletics administrator asked him what he planned to do if he won eight gold medals in Beijing, he shrugged his shoulders. Retirement after London in 2012 was followed by a reality television show, playing golf and a lack of direction.

This time, Phelps insists it’s different. He will no longer have swimming to keep his life within the boundaries — to bring him happiness, contentment, sobriety. But he doesn’t need it. He finally knows who he is beyond a swimmer. He no longer needs gold medals to define himself as a successful and productive human being. He has a fiancée who has been there through the good times and bad and loves him for the man, not the medals. He has a son who will blindingly care about him and has cried in recent days during FaceTime chats because he misses Dad.

This time, Rio was closure.

“He knows where he’s going,” Bowman said. “There is a baby and a house and a wife. He can see the next chapter of his life. And he can’t wait. I don’t worry about him anymore. He’s on a great path, and I think that’s the biggest difference.”

The end of Phelps’ competitive career is anything but the end of his story. In many ways, it is only the beginning.

“The things I’ve gone through in my life — this is how I’m supposed to be today,” Phelps said. “I wouldn’t change anything that got me here. I can honestly say this is the best place I’ve ever been in my life.”