The Swiss third seed, a five-time champion in New York, produced 19 unforced errors to surprisingly drop the first set against a player ranked 190th and without a tour-level win.
The 38-year-old recovered to progress 4-6, 6-1, 6-2, 6-4 and clinch his 1 224th career victory that set up a meeting with Bosnia's Damir Dzumhur.
"Maybe it's not a bad thing to go through a match like this. It was very similar at Wimbledon when I dropped the first set there, as well, in the first round," said Federer, who lost a five-set epic to Novak Djokovic in last month's final at the All England Club.
"At the end you look at the last three sets, and they were good. That's encouraging."
A dismal first set served as a wake-up call for Federer, who is now 62-0 in Grand Slam first rounds dating back to the 2003 French Open.
"I broke every time first game each set. That was good. I think trying to forget the first set is never sort of easy I guess in a first round, under the lights. People expect a different result. I expect something else," he said.
"I just wanted to pick up my game really, start to play better. I was able to do that. That was a relief, going up 3-0 in the second set, realising that it is in my racquet, how I also felt it in the first set.
"The thing is I wasn't serving consistently enough. I was hitting double-faults that usually I don't do. Also I was just hitting too many unforced errors. I was in two minds, I guess."
Federer's victory saw him qualify for a record-extending 17th ATP Tour finals appearance, although he wobbled as he served for the match in the fourth set, a struggle reminiscent of last month's Wimbledon final when he squandered two championship points.
"I thought he was getting a bit down on himself naturally after set two and three. That's why it was key to stay ahead, and I did," Federer said.
"You have to get over the finish line. I got that the hard way in Wimbledon. He did a good job to stay with me. I had to close it out. That was a tough last game. Maybe exactly the kind of service game I needed to serve it out."
US Open results on Monday (x denotes seeding):
Men
First round
Novak Djokovic (SRB x1) bt Roberto Carballes Baena (ESP) 6-4, 6-1, 6-4
Juan Ignacio Londero (ARG) bt Sam Querrey (USA) 3-6, 6-1, 7-6 (7/3), 7-5
Denis Kudla (USA) bt Janko Tipsarevic (SRB) 3-6, 6-1, 7-6 (7/5), 6-1
Dusan Lajovic (SRB x27) bt Steve Darcis (BEL) 7-5, 6-3, 6-3
Stan Wawrinka (SUI x23) bt Jannik Sinner (ITA) 6-3, 7-6 (7/5), 4-6, 6-3
Jeremy Chardy (FRA) bt Hubert Hurkacz (POL) 3-6, 6-3, 6-7 (6/8), 6-1, 6-4
Miomir Kecmanovic (SRB) bt Laslo Djere (SRB) 6-2, 6-1, 7-5
Paolo Lorenzi (ITA) bt Zachary Svajda (USA) 3-6, 6-7 (5/7), 6-4, 7-6 (7/4), 6-2
Reilly Opelka (USA) bt Fabio Fognini (ITA x11) 6-3, 6-4, 6-7 (6/8), 6-3
Dominik Koepfer (GER) bt Jaume Munar (ESP) 6-4, 7-6 (7/2), 5-7, 7-5
Jenson Brooksby (USA) bt Tomas Berdych (CZE) 6-1, 2-6, 6-4, 6-4
Nikoloz Basilashvili (GEO x17) bt Marton Fucsovics (HUN) 3-6, 6-4, 6-2, 3-6, 6-3
Feliciano Lopez (ESP) bt Taylor Fritz (USA x26) 3-6, 6-4, 6-3, 6-4
Yoshihito Nishioka (JPN) bt Marcos Giron (USA) 3-6, 6-4, 6-4, 6-4
Hugo Dellien (BOL) bt Kwon Soon-woo (KOR) 6-3, 6-4, 2-6, 2-3 retired
Daniil Medvedev (RUS x5) bt Prajnesh Gunneswaran (IND) 6-4, 6-1, 6-2
Roger Federer (SUI x3) bt Sumit Nagal (IND) 4-6, 6-1, 6-2, 6-4
Damir Dzumhur (BIH) bt Elliot Benchetrit (FRA) 4-6, 6-2, 6-3, 6-0
Dan Evans (GBR) bt Adrian Mannarino (FRA) 6-4, 6-3, 2-6, 6-3
Lucas Pouille (FRA x25) bt Philipp Kohlschreiber (GER) 6-3, 4-6, 6-4, 6-4
Pablo Carreno Busta (ESP) bt Guido Pella (ARG x19) 6-3, 4-6, 7-6 (7/2), 6-3
Ricardas Berankis (LTU) bt Jiri Vesely (CZE) 4-6, 7-6 (7/4), 3-6, 7-6 (7/4), 6-4
Gregoire Barrere (FRA) bt Cameron Norrie (GBR) 7-6 (7/4), 6-4, 4-6, 6-7 (5/7), 7-6 (7/2)
David Goffin (BEL x15) bt Corentin Moutet (FRA) 6-3, 3-6, 6-4, 6-0
Borna Coric (CRO x12) bt Evgeny Donskoy (RUS) 7-6 (9/7), 6-3, 6-0
Grigor Dimitrov (BUL) bt Andreas Seppi (ITA) 6-1, 6-7 (2/7), 6-4, 6-3
Pablo Cuevas (URU) bt Jack Sock (USA) 6-4, 7-5, 7-6 (7/5)
Kamil Majchrzak (POL) bt Nicolas Jarry (CHI) 6-7 (2/7), 7-6 (7/5), 7-6 (8/6), 1-6, 6-4
Cristian Garin (CHI x31) bt Christopher Eubanks (USA) 3-6, 7-6 (7/5), 6-4, 6-7 (4/7), 6-3
Alex de Minaur (AUS) bt Pierre-Hugues Herbert (FRA) 6-4, 6-2, 6-7 (6/8), 7-5
Bradley Klahn (USA) bt Thiago Monteiro (BRA) 6-3, 6-2, 6-3
Kei Nishikori (JPN x7) bt Marco Trungelliti (ARG) 6-1, 4-1 retired
Women
First round
Elina Svitolina (UKR x5) bt Whitney Osuigwe (USA) 6-1, 7-5
Venus Williams (USA) bt Zheng Saisai (CHN) 6-1, 6-0
Rebecca Peterson (SWE) bt Monica Puig (PUR) 6-3, 6-3
Dayana Yastremska (UKR x32) bt Monica Niculescu (ROM) 6-4, 1-6, 6-2
Sofia Kenin (USA x20) bt Coco Vandeweghe (USA) 7-6 (7/4), 6-3
Laura Siegemund (GER) bt Magdalena Frech (POL) 5-7, 6-3, 6-4
Zhu Lin (CHN) bt Wang Xinyu (CHN) 6-3, 6-4
Madison Keys (USA x10) bt Misaki Doi (JPN) 7-5, 6-0
Johanna Konta (GBR x16) bt Daria Kasatkina (RUS) 6-1, 4-6, 6-2
Margarita Gasparyan (RUS) bt Priscilla Hon (AUS) 7-6 (7/4), 6-4
Ekaterina Alexandrova (RUS) bt Samantha Stosur (AUS) 6-1, 6-3
Zhang Shuai (CHN x33) bt Viktorija Golubic (SUI) 6-2, 6-1
Karolina Muchova (CZE) bt Elena Rybakina (KAZ) 6-4, 6-4
Ons Jabeur (TUN) bt Caroline Garcia (FRA x27) 7-6 (10/8), 6-2
Aliaksandra Sasnovich (BLR) bt Jennifer Brady (USA) 6-1, 4-6, 6-0
Mariam Bolkvadze (GEO) bt Bernarda Pera (USA) 6-3, 5-7, 6-4
Karolina Pliskova (CZE x3) bt Tereza Martincova (CZE) 7-6 (8/6), 7-6 (7/3)
Karolina Muchova (CZE) bt Elena Rybakina (KAZ) 6-4, 6-4
Serena Williams (USA x8) bt Maria Sharapova (RUS) 6-1, 6-1
Caty McNally (USA) bt Timea Bacsinszky (SUI) 6-4, 6-1
Hsieh Su-Wei (TPE x29) bt Jana Cepelova (SVK) 6-4, 5-7, 6-3
Petra Martic (CRO x22) bt Tamara Zidansek (SLO) 6-4, 4-6, 6-1
Ana Bogdan (ROM) bt Harriet Dart (GBR) 6-3, 6-1
Iga Swiatek (POL) bt Ivana Jorovic (SRB) 6-0, 6-1
Anastasija Sevastova (LAT x12) bt Eugenie Bouchard (CAN) 6-3, 6-3
Kristina Mladenovic (FRA) bt Angelique Kerber (GER x14) 7-5, 0-6, 6-4
Fiona Ferro (FRA) bt Daria Gavrilova (AUS) 6-3, 6-4
Alison Van Uytvanck (BEL) bt Viktoria Kuzmova (SVK) 6-4, 6-4
Wang Qiang (CHN x18) bt Caroline Dolehide (USA) 6-4, 6-4
Maria Sakkari (GRE x30) bt Camila Giorgi (ITA) 6-1, 6-0
Peng Shuai (CHN) bt Varvara Lepchenko (USA) 6-2, 7-6 (7/5)
Lauren Davis (USA) bt Johanna Larsson (SWE) 7-5, 6-2
Ashleigh Barty (AUS x2) bt Zarina Diyas (KAZ) 1-6, 6-3, 6-2