The six-time champion overcame a two-set deficit to avert a monumental first-round upset, beating Alejandro Falla 5-7, 4-6, 6-4, 7-6 (1), 6-0.
Federer has reached the tournament final each of the past seven years, but Monday he barely survived the traditional opening match on Centre Court as defending champion.
"I live another day," Federer said. "This one is one I should have lost. That's sometimes how grass-court tennis works."
The 60th-ranked Falla had lost all 11 sets in his previous four matches against Federer, but the Colombian played brilliant tennis to take charge of the match early. The turnaround came in the fourth set with Falla serving for the match and three points from victory, when Federer broke for only the second time.
Federer played his best after that. It was the sixth time he has won after trailing by two sets.
"I definitely got very lucky today out there," he said.
After winning the first two sets, Falla received treatment from a trainer during the next three changeovers for an upper left leg injury. But it was only when he served for the biggest victory of his career at 5-4 in the fourth set that his game unraveled.
Falla made shaky errors on the first two points, and a pair of deft forehands by Federer gave him the break. He ran away with the tiebreaker, taking advantage of four more unforced errors by Falla, and the disconsolate Colombian mustered little resistance in the final set.
There had been signs coming into the tournament that Federer might be vulnerable. He lost at the French Open this month in the quarterfinals, his earliest Grand Slam exit in six years. Then he dropped to No. 2 in the rankings behind nemesis Rafael Nadal. Then at a Wimbledon warm-up event came Federer's second grass-court defeat since 2003, extending his drought of nearly five months without a title.
But no one expected so much trouble against a 26-year-old journeyman who has yet to win a tournament. There were stretches of stunned silence from the crowd, dumbfounded by the score. Fans also roared in appreciate of Falla's frequent winners.
"He played great," Federer said. "He was the one who put me in that kind of a score. I thought I was actually playing decent. Credit to him."
The match was Falla's third in the past four weeks against Federer, which at first worked to the Colombian's advantage. He kept Federer off balance by coming to the net often and made good use of crosscourt shots from the baseline.
The left-handed Falla was unfazed by Federer's serve, one of the sport's best, and repeatedly won points serving to Federer's backhand — a tactic frequently employed by another lefty, Nadal.
Federer searched for more than two hours to find his championship form. He slipped several times on the immaculate lawn and shanked shots, hitting one forehand so wild that Falla had to leap out of the way.
The tournament began under partly cloudy skies, with temperatures headed into the mid-70s, but things quickly turned gloomy for Federer. He was 0-for-6 on breakpoint chances before putting a forehand winner on the line to close out the third set.
He lost serve to start the next set, and found himself on the verge of defeat with Falla serving at 5-4.
Then Federer's big surge began. Barely 30 minutes later, he kissed the line with his final shot for a winner and walked to the net to give Falla a sympathetic pat on the shoulder.