The second-seeded Swiss withstood an early onslaught from the unpredictable Aussie, firing 51 winners that included an unlikely forehand flicked around the net post that Kyrgios couldn't believe.
"Definitely a bit of luck and good feet," Federer said of the shot that had Kyrgios wide-eyed at the net.
Federer had the luxury of a two-set lead when the duo's most entertaining exchanges thrilled the Arthur Ashe Stadium crowd.
Things would have been much different if Kyrgios could have converted one of four break points in the seventh game of the opening set.
But Federer escaped, breaking Kyrgios to pocket the first set and dominating the second.
"He's an unbelievable frontrunner," Kyrgios said. "When he gets in front, there's not much you can do."
Federer will be seeking a 13th appearance in the US Open quarter-finals when he plays Australian John Millman, who reached the last 16 of a major for the first time with a 6-4, 4-6, 6-1, 6-3 victory over Kazakhstan's Mikhail Kukushkin.
If he makes it past Millman, Federer could find Wimbledon champion Novak Djokovic waiting in the last eight.
Djokovic, winner of US Open titles in 2011 and 2015, extended his dominance over Richard Gasquet with a 6-2, 6-3, 6-3 victory in front of a rowdy night crowd on Ashe.
"It was the best match of the week so far without a doubt, and one of the best performances of the hardcourt season after Wimbledon," said Djokovic, who warmed up for Flushing Meadows by beating Federer in the Cincinnati Masters final.
Djokovic saved all five break points he faced to book a meeting with Joao Sousa of Portugal, a 7-6 (7/5), 4-6, 7-6 (7/4), 7-6 (7/5) winner over France's Lucas Pouille.
The third round was the end of the line for men's and women's fourth seeds from Germany -- Alexander Zverev and Angelique Kerber.
Kerber, the 2016 US Open winner tying to become the third woman in the last 20 years to win both Wimbledon and the US Open in the same season, was bundled out by Slovakia's Dominika Cibulkova 3-6, 6-3, 6-3.
Zverev, the 21-year-old sensation, who has lifted titles in Munich, Madrid and Washington this year and reached finals in Miami and Rome, fell 6-7 (1/7), 6-4, 6-1, 6-3 to veteran compatriot Philippe Kohlschreiber.
It was another Grand Slam disappointment for Zverev, touted as the youngster most likely to end the major dominance of Federer, Djokovic and Rafael Nadal.
Kohlschreiber will seek a quarter-final berth against 2014 runner-up Kei Nishikori after the Japanese downed Argentine Diego Schwartzman 6-4, 6-4, 5-7, 6-1.
Kerber's defeat followed the early exits of world number one Simona Halep and No. 2 Caroline Wozniacki -- who were both also seeking a second Grand Slam title of the year.
Fifth seed Petra Kvitova was also ousted, the two-time Wimbledon champion falling 7-5, 6-1 to rising Belarussian star Aryna Sabalenka.
In all just three of the top 10 women made it through to the fourth round, although Kerber didn't blame a domino effect for her departure.
"I'm not looking around who was winning or who was losing," she said. "You have sometimes days where you are trying everything, but it's not really working as you wish."
Cibulkova, the 29th seed, moves on to face 2017 finalist Madison Keys, who shook off a slow start to beat Serbian Aleksandra Krunic 4-6, 6-1, 6-2.
Sabalenka will play Japan's Naomi Osaka, who crushed Aliaksandra Sasnovich 6-0, 6-0.
Sixth-seeded Caroline Garcia of France also departed, beaten 5-7, 6-4, 7-6 (7/4) by Spain's Carla Suarez Navarro, and Czech teenager Marketa Vondrousova sprang a surprise, too, with a 7-6 (7/4), 2-6, 7-6 (7/1) victory over 13th-seeded Belgian Kiki Bertens.
Suarez Navarro, seeded 30th, next faces 2006 champion Maria Sharapova, who defeated 2017 French Open winner Jelena Ostapenko 6-3, 6-2.
Vondrousova, through to a Grand Slam third round for the first time, will play Ukraine's Lesia Tsurenko, who defeated Czech Katerina Siniakova 6-4, 6-0 after taking out Wozniacki in the second round.