Federer recorded his 80th career win in New York but he needed five sets for the second successive match before claiming a 6-1, 6-7 (3/7), 4-6, 6-4, 6-2 triumph over Mikhail Youzhny.
It was his 17th win in 17th meetings with the grizzled Russian, who, at 35, is his junior by just one year.
World number one Nadal, meanwhile, saw off Japan’s world 121 Taro Daniel 4-6, 6-3, 6-2, 6-2 after being blown off court in the first set.
Federer, 36, and the five-time champion at the US Open, next faces another 35-year-old, Feliciano Lopez of Spain, for a place in the last 16. His record against Lopez is just as solid — 12-0.
Youzhny admitted he had cramped, which meant he could move left to right but not forward and back.
“That was hard to watch,” said Federer. “But this was probably the best match we have played against each other.”
Top seed Nadal, the 2010 and 2013 champion, was rocked by New York-born Daniel in the first set.
But Daniel, who has never beaten a top-10 player, paid the price for his all-out assault as Nadal prevailed to book a third round date with Argentine lucky loser Leonardo Mayer.
He saw room for improvement after converting six of 11 break points.
“I didn’t play very well but I will work hard to find better feelings because I know I can do much better,” he said.
Andrey Rublev became the second teenager to make the third round when he stunned Bulgarian seventh seed Grigor Dimitrov 7-5, 7-6 (7/3), 6-3.
Rublev, just 19 and the world number 53, joins 18-year-old Denis Shapovalov of Canada in the last 32 at a Grand Slam event for the first time.
However, 19-year-old Taylor Fritz of the United States was unable to join Rublev and Shapovalov in the next round when he lost 6-4, 6-4, 4-6, 7-5 to Austrian sixth seed Dominic Thiem.
Thiem saved 16 of 18 break points he faced in a stalwart defensive display.
Also going through to the last 32 was 2009 champion Juan Martín Del Potro, the 24th seeded Argentine, who saw off Spanish qualifier Adrian Menendez-Maceiras 6-2, 6-3, 7-6 (7/3).
Belgian ninth seed David Goffin beat Guido Pella of Argentina 3-6, 7-6 (7/5), 6-7 (2/7), 7-6 (7/4), 6-3 in a marathon 4hr 12min match.
– Women play longest match –
A record was set when American Shelby Rogers and 25th-seeded Australian Daria Gavrilova played out the longest ever women’s singles match.
Their 3hr 33min clash saw Rogers win 7-6 (8/6), 4-6, 7-6 (7/5).
Their second-round tie went past the previous longest of the 3 hours and 23 minutes it took Johanna Konta to beat Garbine Muguruza in the second round two years ago.
Russian eighth seed Svetlana Kuznetsova, the 2004 champion, saw her outside chances of taking the world number one ranking end when she lost to Japan’s Kurumi Nara 6-3, 3-6, 6-3.
Kuznetsova is the fifth of the top eight seeds to lose before the third round after Simona Halep, Caroline Wozniacki, defending champion Angelique Kerber and Johanna Konta also exited.
World number one Karolina Pliskova, the 2016 runner-up, downed American qualifier Nicole Gibbs 2-6, 6-3, 6-4.
She was joined in the third round by fourth seed Elina Svitolina, one of the pretenders to her top ranking, and French Open champion Jelena Ostapenko.
Ukraine’s Svitolina reached the last 32 for a third successive year by breezing past Russia’s Evgeniya Rodina 6-4, 6-4.
Ostapenko, the 12th-seeded Latvian, made the third round for the first time with a 6-4, 6-4 win against Romania’s Sorana Cirstea.