The Spanish second seed led by two sets and a break, but he needed to halt a thrilling comeback from Russian rebel Medvedev to win an epic showdown 7-5 6-3 5-7 4-6 6-4.
Hot-headed Medvedev was playing in his first grand slam final, having revelled in his new role as public enemy number one of the New York crowd.
The 23-year-old had been jeered by those in the stands throughout the fortnight after he flipped them the middle finger during the third round.
Yet when he launched an unlikely counter-attack in the third set it was like Rocky in reverse, the fans around a packed Arthur Ashe suddenly chanting his name.
However, Nadal dug as deep as he has probably ever had to to secure a dramatic victory in four hours and 50 minutes to move to within one major title of Roger Federer’s record tally of 20.
Nadal, whose hackles were raised when he was given a time violation at break point down in the very first game, took the opening set with a break at 6-5 thanks to a whipped forehand winner and a lob which Medvedev dumped into the net.
The Russian saved four break points in his next two service games but the jabs kept on landing, Nadal breaking through again for 4-2 and taking a two-set lead.
Fatigue seemed to be setting in for Medvedev, who was being pushed all around the court, and when a backhand hit the net at break point in the fifth game of the third set he looked beaten.
But he kept swinging, somehow breaking back to level, and with the crowd now firmly behind their new darling, he repeated the dose at 6-5 to take it to a fourth set with Nadal now the man looking vulnerable.
Gutsy hold after gutsy hold followed until Nadal sent a forehand wide and Medvedev had a break point, which he dispatched with a stunning return to force a decider.
It was sensational stuff, but the odds were still stacked against Medvedev. He had never before won a five-set match and Nadal had lost only twice from two sets up in his career.
Nadal’s delaying tactics were irking the Russian but he fashioned two break points, then a third, only for Nadal to escape again.
When Nadal eked out a break point of his own at 2-2, Medvedev aced him. But when Nadal created another, a double-handed backhand putting him back in control.
Still the drama continued.
Nadal broke again but, serving for the match, he was given a second time violation by umpire Ali Nili and docked a serve at break point down, and promptly sent his second serve wide. Umpiring controversy is not confined to Serena Williams here, it seems.
Medvedev saved two match points on serve but when Nadal had the ball in his hand again he finally got over the line, a long Medvedev return bringing to an end a truly remarkable final.
An exhausted Nadal was in tears as he sat in his chair waiting for the post-match presentations.
He admitted: “It’s been one of the most emotional nights in my tennis career.
“It was an amazing final, since I had the match under control more or less. Tonight everybody saw why he is number four in the world.
“The way he was able to fight, to change the rhythm of the match, was incredible.”
Medvedev addressed his new admirers. He had goaded them after his controversial win over Feliciano Lopez, saying “I want all of you to know, when you sleep tonight, that I won because of you.”
This time he said: “I want to talk about you guys. Earlier I said it in a bad way, now I mean it in a good way. It’s because of your energy I’m here in the final.”
He added: “Congratulations to Rafa, 19 grand slams is unbelievable, outrageous.
“I was looking at the screen when they were showing number one, number two, number 19, and I was like, ‘if I won, what would they show?’
“In the third set to be honest I was already thinking about what do I say in the speech, it’s going to be in 20 minutes. So I had to fight for every ball and see how far it goes.”