The top-seeded Federer broke serve straight away and then broke again to close out the first set. He missed two match points when 5-6 up in the second set before taking control of the second set tiebreaker to win in 1 hour, 21 minutes.
Federer is seeking to improve his game in Madrid. He lost his first clay-court match of the year at the Rome Masters before exiting the Estoril Open at the semifinal stage.
The Swiss has reached the final in three of the last four years and is hoping Madrid will galvanize his season as it did last year, when he went on to win the French Open and Wimbledon.
"I'm happy with the way I reacted after all that happened in the last few weeks and months," Federer said. "The moment the match started, I felt great. I was hitting the ball fine and I think the center court here just feels right to me _ I've never lost on it, so that gives me confidence and I think it showed today in my game."
Federer will play either Stanislas Wawrinka or Julien Benneteau.
In the first round, 16th-seeded Thomaz Bellucci beat Pere Riba of Spain 7-6 (5), 6-1.
Wild card David Nalbandian, the champion in 2007, and his opponent Tomas Berdych both withdrew due to separate injuries before their match. The organizers did not specify the nature of the injuries.
Michael Russell and Mardy Fish, two players who narrowly failed to get through the qualifying phase, were drafted in as replacements.
Juan Ignacio Chela, Eduardo Schwank, Philipp Petzschner Ernests Gulbis, Marcos Baghdatis, Stanislas Wawrinka and Victor Hanescu also progressed to the second round.
On the women's side, 2009 finalist Caroline Wozniacki was beaten 6-2, 6-3 in an upset by 26th-ranked Alona Bondarenko. The Ukrainian will play Li Na of China in the third round.
In other second-round matches, Patty Schnyder beat ninth-seeded Agnieszka Radwanska 3-6, 6-4, 6-4, and Anabel Medina Garrigues defeated 12th-seeded Marion Bartoli 6-2, 6-0.
Andrea Petkovic of Germany downed 14th-seeded Flavia Pennetta 7-6 (3), 6-3 on her debut in Madrid.
Eighth-seeded Samantha Stosur edged Italian Open Champion Maria Jose Martinez Sanchez of Spain 7-6 (2), 6-4. The Australian has now won 13 of 14 matches on clay this season.
"I don't think I could've asked for a much better preparation going into the French," Stosur said. "To get at least three matches here is great, so everything from here on is a bonus. If it ends up being a few more this week, then that's great."
Nadia Petrova, seeded 16th, beat Anastasia Pavlyuchenkova 6-2, 7-5 in an all-Russian match.
Other winners were Shahar Peer of Israel and Lucie Safarova of the Czech Republic.