In a final that had pretty much everything, Rafael Nadal turned back the challenge of the dazzling young Russian, Daniil Medvedev, in five sets here on Sunday night to win his fourth US Open and edge to within one slam of Roger Federer’s collection of 20.

But what a struggle it was. Medvedev, in his first grand slam final and not having won a five-setter in four attempts, announced his arrival with a performance of the highest daring; Nadal, the king of long-haul tennis, sent the message that, at 33, there is life in his legs and his heart yet.

So, the Spaniard remains on schedule to pass his ageing Swiss friend after a 7-5, 6-3, 5-7, 4-6, 6-4 win against the fifth seed over four hours and 50 minutes – the longest match of the fortnight - on a warm evening in front of 24,000 bedazzled fans in the Arthur Ashe Stadium.

“It has been an amazing final,” said Nadal after the match. “[Medvedev’s] summer is one of the best in the sport since I started playing. The way that he was able to fight was just incredible. It was the most emotional night in my tennis career. The nerves were so high. It was a crazy match.”

Medvedev paid tribute to his opponent in his post-match interview. “Unbelievable, outrageous, Rafa winning his 19th slam title It’s amazing for our sport what you’ve done. I fought for every point but unfortunately it didn’t go my way,” he said.

At times, early on, it looked easy; mostly it did not – especially in the latter stages when Medvedev fashioned a revival from nowhere that rattled Nadal and won over New Yorkers who had booed his boorishness in the first week. They became converts to his cause midway through the third set, when Nadal botched a smash to let Medvedev back in the match and, in the fourth and fifth sets, where the players went shot-for-shot to the end.

What had looked to be a routine win for Nadal ended in a riot of nerves and glorious shot-making, one of the best slam finals in years.

Nadal has now won five majors since turning 30. If he were a wine, he’d be a fine grand reserve rioja. His 19th major means the Big Three – Nadal, Federer and Novak Djokovic – have won 51 of the 59 played since he won his first in 2005. But Medvedev did his best to break the hegemony last breached by Stan Wawrinka, who beat Djokovic here in 2016.

It was Nadal’s 27th slam final, and he will be encouraged to believe there are more to come. He has never had much luck in Melbourne, although he won there in 2009, while Roland Garros, where he won his 12th French Open title this year, remains his banker, and he will have drawn comfort from reaching the semi-finals at Wimbledon two months ago.

Nadal has suffered more physically over the years than Federer or Djokovic, but he held everything together here just as they hobbled from the fray, injured. In the absence of the revered Swiss, Nadal was the crowd’s early favourite against Medvedev, an ebullient performer with a bewildering arsenal of skills and a gift for winding up the crowd. They hated him at the start of the tournament for his on-court petulance and warmed to him when he said he fed off their hostility.

Indeed, Medvedev paid tribute to the crowd after the match. “Because of your energy I was here in the final,” he said. “I’m a human being, I can make mistakes. Thank you very much from the bottom of my heart.”

Nevertheless, the court, the title and the tough New York crowd belonged to Nadal in the end. To put his win in context, Medvedev has won 12 matches in a row since losing 6-3, 6-0 in the Montreal Masters last month – against Nadal.

Nadal took eight minutes to hold at the start, saving his first break point as Medvedev applied educated pressure from the back of the court.

The 23-year-old Russian is the youngest finalist here since Djokovic lost to Nadal in 2010. Nadal has tweaked his muscular tennis since that match, but he found Medvedev equal to many of his questions, and dropped serve in the third game with a limp forehand from deep.

When he saw his opponent struggle with a wide slice at the end of a long rally in the fourth game, he forced a weary forehand from him to break back. Nadal held to love for 4-3 - and the crowd cheered when he changed his shirt. They never had the opportunity to do that with Federer.

They began to warm to the gawky elegance of Medvedev, more heron than gazelle. Nadal held to love, with an ace, then ground down the Russian’s serve with a mixture of spin and muscle off the ground. A teasing backhand forced the elastic Medvedev to twist in the air, but he could not control the difficult volley and the first set was gone in just over an hour.

Daniil Medvedev congratulates his opponent after the match. Photograph: Charles Krupa/AP

Nadal had to save break point to hold in the second, but the energy and momentum was with him and he forced a backhand error from Medvedev to go 4-2 up. The set was quickly in Nadal’s pocket.

Medvedev, who had been half in the contest, half out, came to life when he recovered from a break to parity in the sixth game of the third, then launched a rousing fightback. The Russian turned the mob into hysterical fans when he broke to force a fourth set.

Nadal suddenly was under intense pressure, saving break point in the second game. There was little in it for the next 50 minutes, but Nadal, trailing in the serving cycle, was the one hanging on in many rallies, and Medvedev grabbed set point, clinching it with an extraordinary winner off his first serve.

Then several twists significantly altered the drama.

In the second game of the fifth, the crowd suddenly turned on Nadal, who complained they were interrupting him; Medvedev then had the trainer on to massage his left thigh; and, three games later, Nadal broke from 40-0 down after a closing 28-shot rally that left Medvedev spent.

Now Nadal was in the same position as in the third set, when he butchered that smash. This time after putting the simplest of volleys into the net at 40-0, he held, tapping a backhand away for 4-2. Medvedev smashed long and Nadal served for the championship. Then came more Medvedev magic as he scrambled a break point – at which point Nadal lost his serve when the shot clock timed him out.

Serving to keep the match alive, Medvedev couldn’t reach a drop shot and Nadal had match point after four hours and 38 minutes. A blizzard of mistakes and winners followed, Medvedev survived and Nadal went to the service line again.

He cut it fine with the shot clock a couple of times again, framed a forehand to hand Medvedev a break point, but the Russian hit long. Nadal drop-shotted at deuce, a seriously brave move, and was back on match point for the third time. Medvedev hit long again and it was done.

The last time Nadal saw off a Russian prospect here – 19-year-old Andrey Rublev two years ago in the quarter-finals – the Spaniard, 31 at the time, said, “I always wanted to be young. Even when I was eight years old, I was not very happy to be nine. I don’t want to get older. For the moment, I didn’t find the way to stop that watch.”

Maybe he did.