With a 4-3 lead, Federer secured the opening break of the match to allow himself to serve out the first, although he had to save three break points as the Spaniard fought hard to keep himself in the set.
Ferrer continued to pose a threat to the Swiss's serve at the start of the second, breaking his opponent twice on his way to a 4-0 lead.
Federer held serve to get on the board in the sixth game of the set, but the world number six tied the match up in the next game with a comfortable hold.
After the world number three opened up a commanding lead in the decider, Ferrer had to serve to stay in the match at 5-2 down, but a string of well-timed returns from Federer brought up two match points, which he converted at the first time of asking.
The win is Federer's first in a Masters event for two years.