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.
US Open best chance to unseat 'Big Three', says Djokovic
"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."
Who's who? A guide to US Open stars' first round rivals
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, realizing 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."
COMMENTS
Comments are moderated and generally will be posted if they are on-topic and not abusive.
For more information, please see our Comments FAQ