It is unlikely the glistening possibility of a golden slam (ownership of all four majors and an Olympic gold medal) blinded Roger Federer in sight of the prize. He is too cool for that. Juan Martín del Potro had no such distractions but, after an heroic effort, he could not survive the examination of a record-breaking semi-final and the Swiss, betraying occasional nerves between breathtaking ground strokes and 24 aces, won 3-6, 7-6, 19-17 in four hours and 26 minutes.

The final set took two hours and 43 minutes. Legs held up, spirits soared and dipped, and records tumbled.

The match was half an hour longer than the previous longest Olympic singles, between Jo-Wilfried Tsonga and Milos Raonic in the third round here. It was also the longest three-set singles match in the Open era, beating the four hours and three minutes of the 2009 Madrid semi-final between Novak Djokovic and Rafael Nadal.

Federer has not been detained this long in a single set since he finished off Andy Roddick 16-14 to win Wimbledon three years ago. That whole match, over five sets, took four hours and 16 minutes, ten minutes shorter than Friday's struggle over three. It also exhausted the ink in a colleague's pen; that's long.

Del Potro's brutal shotmaking kept Federer deep in the first set, and he grabbed it 6-3.

Federer had lost the first set 12 times in 52 matches this year, and recovered to win eight of those. Del Potro had lost to Federer five times in 2012, with only the memory of his win en route to his sole slam victory three years ago in New York to sustain his belief that, on his day, he has the measure of the Swiss. Friday was not quite one of those days and he broke down in tears after his final shot, a weary backhand, hit the net.

"To lose this way hurts a lot," he said. "It's very hard to talk about it right now. Everyone has their time. The US Open was my time, not today."

Federer was generous in victory: "Juan Martín did so well to hang in there. I don't think I have ever played as long a set in a best of three-set match. It was very physical at the end and so mental. I feel horrible for Juan Martín, but he can be very proud."

Del Potro had three break points in the second set, Federer two, and Federer's serve, clicking beautifully in the tie-break, levelled it at a set apiece.

Then they got down to business in their contrasting ways.

Moving at times like Mrs Romney's horse – sideways with long legs pumping furiously – Del Potro used his extraordinary levers to retrieve some of Federer's teasing cross-court strokes. While Federer's conservatism governed his daring, Del Potro had to fight hard to retain the shape and distance in his ground strokes.

Federer could not take advantage of break points in the seventh and ninth games, and snatched another break point in the 15th game when del Potro chose the right shot – down the line with his man stranded – but the wrong execution, and it went long. However, Federer wasted the chance, netting a dolly backhand.

Del Potro's second double fault of the match gave Federer another glimpse at 30-all and this time he went on to secure the break when Del Potro mucked up a forehand.

Federer was one game away from the final but, unbelievably, when he belted a backhand on to the net and out of bounds, he had lost his serve to love and they were level again.

Federer had three break points in the 29th game when Del Potro's backhand strayed long, but the Argentinian scrambled back to deuce with shots that burned the chalk on either side of the oblong, and held with another glorious forehand that clipped the baseline.

In the 35th game, Federer had three more break points. This time, he did not blow it. Del Potro did.

Serving for the match for a second time, Federer surrendered to the joy of the moment when Del Potro gave him match point – then squandered it with netted backhand. A forehand winner down the line delivered him another shot at victory, Del Potro's concentration snapped for the last time and it was done.