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.
"It was quite a lot of fun out there -- I feel quite warmed up by now," said Australian Open and Wimbledon champion Federer, who fired 63 winners and 68 unforced errors.
Also Read
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."
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.
"All the matches are difficult but especially here as everyone wants to play their best and if you don't play your best it will be very difficult," said Nadal, who recovered from a set and break down.
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.
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).
- 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.
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.
Disclaimer: No Business Standard Journalist was involved in creation of this content