Ever better as the going gets tougher, American Michael Phelps roared back from seventh at the turn in the last solo race of his towering career to a 51.21 victory to become the first swimmer ever to win the same title at three Games in two events.

The layers of Phelps' accomplishments go as deep as the sea bed, his latest triumph delivering a 17th Olympic career gold and 21st medal of any colour in the ultimate pool. Today the 27-year-old Baltimore Bullet will fire one last time before he retires, another gold expected to close his account at 18 wins and 22 medals in all.

For the first time ever in the 100m butterfly, the silver was shared, in 51.44 by Chad Le Clos and Russian Evgeny Korotyshkin of Russia. The tables were turned on Le Clos, the 20-year-old South African who beat the American by 0.05sec in what had been Phelps' signature tune, the 200m.

Others might have been knocked sideways by such a loss but Phelps' other mighty signature has survived these swansong Games: no other in the sport has had the ability to devour defeat and disappointment as fodder for the next fight quite like the greatest of greats.

The day after being denied by Le Clos, Phelps was back at his best, defeating teammate and world champion and record holder Ryan Lochte in the 200m medley to become the first man in history to win the same crown three times. Victory made him the first man ever to win the same title at three Games. Emotions in check, he was back half an hour later to fire a 50.86sec warning shot in the 100m 'fly semi-finals.

In the battle proper last night, US-based Serb Milorad Cavic, the nearly man of Beijing who missed gold by 0.01sec behind Phelps, turned in his customary leading role, on 23.57, Phelps in 7th in 24.35. Off the wall, Phelps surged and within 15 metres had four men caught. With 20m to go, he had Cavic caught, Another line in history in his sights, Phelps rolled on, Le Clos trying to catch the wave. This time, something about the reigning champion's demeanour screamed from Tolkien's book of balrogs: you shall not pass.

Milorad Cavic (SRB), silver by 0.01sec in 2008, was locked out in 51.81 by former training partner at the Andrea Di Nino Project, Korotyshkin, and Le Clos, coached by Graham Hill.

In 51.21sec, Phelps celebrated the biggest margin of victory, 0.23sec, of any of his three wins: in Athens, he beat teammate Ian Crocker by 0.04sec and four years later looked for all the world as though he had lost until driving his hands into the timing pad like a spear, his momentum providing the force needed to stop the clock 0.01sec ahead of Cavic to keep alive his ambitions of winning a record eight gold medals.

Born and raised in the Rodgers Forge neighbourhood on the outskirts of Baltimore and training with coach Bob Bowman by 11, Phelps was sent swimming because he had too much energy and didn't know what to do with it. There was no greater decision in the history of formative moments in Olympic sport.

Coach Bob Bowman found a way to make the boy think twice after he emerged from one session as a youngster and said "I don't get tired". If the training was tough the measures to hone a man capable of coping with anything included stepping ion his goggles when he was 12 to teach him he could race without them and making sure there was no dinner left when he got back to the hotel when away from home on competition tour.

Such measures had the support of Debbie Phelps, mother and teacher. “He got it from both sides: there was me here saying 'if you do this, this, this and this, this will happen’, and he would go home at night and she would say 'well, if Bob says that's a good idea, yeah, I'm sure you can do it!" Bowman said. "She was always willing to go to the next level as he improved and I think that he was really buoyed by that faith."

Mama Phelps bowed her head down in an act of praise and prayer as the race ended last night: her son was done. And what a stunning job he has done. The depth of emotion - and the control of those required - is hard to fathom.

"This was a bigger margin of victory than the last two combined, so we can smile and be happy," Phelps said. "It was fun."

"I'm just happy that the last one was a win," Phelps said. "That's all I really wanted coming into the night."

On the emotions of it all, he said: "I thought it would hit me harder than it is right now, a lot of those emotions haven't gone through my brain over the last week. Once I am done and once tomorrow is over, I think a lot more emotion will come out. I am in meet mode at the moment, you start and it's over. My start of the meet wasn't what we wanted but I picked up some steam and was able to finish with two individual golds. You can't really finish much better, so I am really pleased about how it ended."

Cavic was gracious in defeat, saying: "He's the king of the Olympics Games. I cannot be compared to Michael Phelps. I'm a one-trick pony."

At 18 in Barcelona at the world championships he took silver behind the first sub-51sec effort, delivered by teammate Ian Crocker in the 100m. The clock, 50.98, and defeat challenged the teenager. Then at US Olympic trials in 2004, Crocker took it on still further, in 50.76.

In Athens at the 2004 Olympic Games, the Midas Touch well on its way to being linked at the hip with Phelps hit back: the world record remained with Crocker but the big-time racer won the big race: Phelps turned fifth, Crocker first, and then started to claw back at the pure sprinters, getting to the wall in 51.25, just 0.04sec ahead of Crocker. After the race, Phelps gave up his place in the 4x100m medley final saying that he wanted to see his teammate race to a gold medal.

Four years on in Beijing, beyond losing to Crocker at the 2005 world championships and defeating Crocker for the 2007 global title, Phelps arrived in Beijing a man on an eight-gold-medal mission. His target came under fire from Cavic with a 50.76 Olympic record in the semi final. Bowman worked out that Phelps could be no more than 0.7sec behind Cavic at the turn if he was to win.

At the turn, the clock had Cavic on 23.42; Phelps, 24.04 in 7th, as it was tonight in London, though the clock read 24.35 for his last solo hurrah. Game on in Beijing - but only just. The scoreboard flickered: Phelps 50.58; Cavic 50.59. Serbia protested when it saw the TV slowly. It looked like a Cavic win but where one mad glided in, the other punched a hole through the pad. Conspiracy theories ran wild: Phelps was sponsored by Omega, the company that clearly had hired a little chap to sit out back fiddling with the clocks. Or perhaps not.

There was doubt or controversy tonight. Phelps nailed in, the greatest of the great getting greater as the end of the greatest career is nigh.

Le Clos, meantime, said it was an honour to be up there in the mix at such a moment in history. "When I was racing I was racing for the gold but I'm just thankful that I had the opportunity to race another Olympics final and live my dream of racing with Michael," he said. "But he got me this time."

For the last time too. Phelps smiled, his job soon to pass to passing interest: "It's going to be fun to watch Chad and see what he does over the next couple of years."