Medvedev, who had never reached even the fourth round of a major tournament until last week, was the best player on tour during the summer hardcourt season, reaching four straight finals and going 20-3.

Medvedev is a frustrating opponent who capitalizes on unpredictability with a variety of shots, mixing speeds, spins and drop shots along with a hard, flat backhand. But when he was down two sets and a service break in the third Sunday, the tactical thought foremost on his mind was how would he handle his postmatch loser’s interview.

“I was thinking, ‘O.K., in 20 minutes I have to give a speech. What do I say?’” Medvedev recalled.

He ended up speaking nearly three hours later. After his earlier missteps, Medvedev eventually endeared himself to the New York fans, culminating in Sunday’s thriller, and he told them that their positive support provided him with the incentive to keep fighting, no matter how daunting the challenge.

“I knew I had to leave my heart out there for them,” he said.

Playing with newfound abandon, he discovered a different gear, turning a potential rout into a battle of attrition. He broke back against the surprised Nadal and then held for 4-3. Nadal had two break points at 4-4, but Medvedev fought them off, the highlight a 28-shot rally that took both players to and fro until Nadal hit a forehand into the net.

That elicited cheers from the fans, who chanted Medvedev’s name just a week after his brief villainous turn, and he rode the momentum almost until the very end of the match, which required 341 total points.

“I managed to give huge fight to one of the best players in the history of our sport,” he said. “Have to give myself credit.”

With Nadal serving at 5-6 in the third set, Medvedev won the first three points, including a sizzling forehand down the line at 0-15 that signaled his new aggressive intent and caused the crowd to erupt. Then, at 15-40, he charged in to hit a backhand winner and take the set.