The 31-year-old, top-seeded Federer won 3-6, 6-3, 6-2 against the world number 58, who was celebrating his 26th birthday, to reach the third round of a tournament he last played in 2008.
It was his 30th career win at the tournament.
"I think it was good for me to have a tough match like this and come through at the end," said Federer.
"Clearly these kind of matches are always important, especially losing the first set makes things difficult. You're not allowed to make many more unforced errors in crucial moments and I was able to come through and play better as the match went on. That's always encouraging for the next round."
The Swiss great is using this event and the Gstaad tournament, which starts on July 22, to bounce back from his shock Wimbledon exit ahead of next month's US Open.