The 19-time Grand Slam champion outlasted 70th-ranked US 19-year-old Frances Tiafoe 4-6, 6-2, 6-1, 1-6, 6-4 in a shockingly tough battle that boosted his hopes of a deep run on the New York hardcourts.
"I think this is going to give me a lot of confidence," Federer said. "It's important to go through all of these emotional roller coasters early. That pressure is not fun but it's what you have to go through."
The match was Federer's first since losing the Montreal final to Alexander Zverev nearly three weeks ago. He withdrew from Cincinnati with a sore back the following week.
"I was worried in the beginning from my back issue from a couple weeks ago but I loosened up," Federer said, calling his back "extremely well" and adding "it's only going to get better from here."
"To get through a five-set match you've got to be healthy. I believe this is going to give me great confidence in my game and my body."
Third seed Federer will next meet either Russia's Mikhail Youzhny, who is 0-16 against him, or Slovenia's Blaz Kavcic, 0-1 against Federer, as the Swiss chases a record sixth US Open crown.
The 36-year-old Swiss, who won this year's Wimbledon and Australian Open crowns, took the US Open titles from 2004-2008.
The intense match came after limited preparation due to his back issues.
"I always knew I was going to come in rusty or not great. I was hoping to start better," Federer said. "I was being too cautious with my movement. In the second set it all started to come together.
"I enjoyed myself even though I was maybe tired and nervous at the end. It was cool to be part of that match. I'm really excited I won."
Tiafoe was disappointed he could not stay atop Federer after breaking in the opening game.
"He won by the skin of his teeth," Tiafoe said. "I felt like when I was playing well, I was controlling most of the rallies. When I was hitting the ball big, he wasn't really doing much except staying steady with me."
But that was enough. Federer has not made a first-round exit in a Grand Slam since the 2003 French Open and has never lost an opener on the New York hardcourts. But Tiafoe tested him.
"It was more than a test," Federer said. "It was exciting. It's why I came to New York as well, to go through these emotions."
It also was Federer's first match under the $150 million Arthur Ashe Stadium roof that became operational last year.
"To see the new addition of the sliding roof, it's wonderful," Federer said. "I'm the lucky guy who got to play through the rain so I'm very happy about the roof."
Asked about the noise level with the roof closed, an issue that drew complaints from top-ranked rival Rafael Nadal, Federer said, "When you guys get going it's even a better atmosphere than we used to have."
"I expected worse because I heard comments like this. I didn't think it was that bad and when the crowd got into it it was really cool... I felt like the energy was back in the building again. It was a lot of fun playing in that atmosphere."
Nadal and Federer have never faced each other at the US Open but could meet in this year's semifinals.
And Federer is the only player who can dethrone Nadal as world number one at the US Open.