The much-billed clash between Michael Phelps and his fellow American swimmer Ryan Lochte in the men's 400m individual medley ended with a resounding victory for Lochte, who left his rival trailing in fourth place.

Lochte was ahead from the beginning and held off challenges from Thiago Pereira and Kosuke Hagino to take gold with a time of 4min 05.18sec. Phelps had to be content with fourth spot and a time of 4:09.28.

Brazil's Pereira finished second on 4:08.86, followed by Kosuke Hagino of Japan on 4:08.94.

Despite the huge cheers that greeted Phelps as he emerged poolside, the race was a foregone conclusion after Saturday morning's heat, which had seen Phelps, the greatest swimmer in Olympic history, almost suffer the humiliation of failing to qualify, edging his way into the final by just seven-hundredths of a second.

Earlier Phelps had confessed that he had not expected his rivals to perform as well as they had in the heats, but said he was pleased to have got a place in the final. "I think the only thing that matters is really getting a spot in," he said. "You can't win the gold medal from the morning." Despite Phelps coming within a whisker of missing a place, Lochte had refused to dismiss his rivals chances, saying: "He's a racer and we're going to do everything we can to go one-two tonight.

Phelps had vowed never to contest the event again after Beijing but, after qualifying in the US trials last month, the Baltimore swimmer decided to give it one last go. While Phelps has already secured 14 Olympic titles, it is Lochte whose star has risen over the last couple of years, culminating in five world championship golds in Shanghai last summer.

Phelps did not attempt to make any excuses for his defeat. "Just a crappy race," he said. "I felt fine for the first 200m. I couldn't really go in the last 100m. I saw Thiago out there and they just swam a better race than me, swam a smarter race than me and were more prepared.

"That's why they're on the medal stand. I don't think the lane has anything to do with it.

"I felt I just couldn't really put myself in a good spot for that race. It's frustrating, sure, but I was under 4:10 three times, twice in the last month, month and a half.

"So, it's okay. It's just really frustrating to start off on a bad note like this. It's pretty upsetting. But I think the biggest thing now is just to try to get past this and move forward.

"I have a bunch of other races and hopefully we can finish a lot better than we started. That's what I'm going to try to do.

"I was lucky to get in. After our heat, I figured, 'I'll be fourth, fifth, sixth – somewhere in there'. The whole last heat was 4:12.

"So I was lucky to get in, had a chance to put myself in a spot to start off on a good note and didn't do it."