NEW YORK — Juan Martin del Potro’s stay at the U.S. Open really should be over. It nearly was.

The 2009 champion at Flushing Meadows somehow kept staving off defeat in the fourth round against No. 6 seed Dominic Thiem on Monday. Del Potro was sick and certainly looked sluggish while dropping the first two sets with little resistance. Then he trailed by a big margin in the fourth set, even facing two match points.

Still, del Potro never gave in or gave up, eventually working his way all the way back on the strength of powerful serves and thunderous forehands to edge Thiem 1-6, 2-6, 6-1, 7-6 (1), 6-4 over more than 3½ hours and set up a quarterfinal showdown against Roger Federer.

“Thanks so much for all the support you gave,” del Potro told the boisterous crowd that regaled the 24th-seeded Argentine with “Ole!” chants. “It helped me a lot. … I won’t forget this match.”

Thiem aided del Potro by playing his worst tennis when he was closest to victory in the fourth set. Thiem led 5-2 and served for the match at 5-3 but got broken. Leading 6-5, he got within a point of winning at 15-40 on del Potro’s serve, but a pair of aces at 127 and 121 mph erased those two chances. The ensuing tiebreaker was dominated by del Potro, who closed it with a booming cross-court forehand winner on the run.

In the fifth set, del Potro converted his second match point when Thiem double-faulted.

When it was over, del Potro raised both arms overhead and threw his head back, enjoying the fans’ adulation, then crossed himself. He joked that he thought he should get a trophy just for winning this one.

It was by far the day’s most enthralling match, with spectators’ roars heard all the way across the grounds at Arthur Ashe Stadium, where Federer was beating Philipp Kohlschreiber 6-4, 6-2, 7-5.

The only bit of intrigue in Federer’s match came after the second set, when he left to take a medical timeout. He said afterward with a laugh that it was so he could get “a bit of a rub on my back – or my bottom – and I didn’t want to do it on court.”

Federer’s back was bothering him before the U.S. Open and restricted his practice time, something he blamed for problems while getting pushed to five sets in the first two rounds last week. But the lopsided win against Kohlschreiber, who never held a break point, was Federer’s second in a row in straight sets.

Federer’s record against del Potro is 16-5. But del Potro won their meeting in the 2009 final in New York in five sets for his only Grand Slam title, ending Federer’s streak of five straight U.S. Open championships – and Federer hasn’t won the trophy since.

The other quarterfinal matchup on that half of the men’s bracket is No. 1 Rafael Nadal against 19-year-old Andrey Rublev, the youngest quarterfinalist at the U.S. Open since Andy Roddick in 2001.

Nadal got to the quarterfinals for the first time since his 2013 title, overwhelming Alexandr Dolgopolov 6-2, 6-4, 6-1.

“Every victory, every set that you win, is more confidence,” Nadal said. “That what’s I am doing.”

The top-seeded woman had even less trouble than Nadal: 2016 runner-up Karolina Pliskova beat 91st-ranked American Jennifer Brady 6-1, 6-0. In the quarterfinals, Pliskova will take on No. 20 CoCo Vandeweghe, who beat Lucie Safarova 6-4, 7-6 (2) to become the third U.S. woman in the next round.

Venus Williams and Sloane Stephens advanced Sunday.