The world number one was in dazzling form at the start of the quarter-final but at a set and 4-2 up his serve deserted him and a series of wayward groundstrokes let the Spanish 12th seed back in.
Ferrer levelled and Federer, who had never lost so much as a set in six previous meetings with Ferrer, only just scraped home.
Federer will continue his preparation for the French Open, the only grand slam he has never won, against another Spaniard after Carlos Moya pulled off a great 7-6 4-6 7-5 win over the fast rising Serb Novak Djokovic, seeded fourth.
Djokovic, aged 19 and with three titles already this season, lost the tiebreak at the end of an untidy first set 7-4 but hit back strongly in the second.
The 30-year-old Moya, who won at Roland Garros back in 1998, grabbed a break at the start of the third and calmly withstood everything Djokovic could throw at him.
SCOOPED FOREHAND
Moya missed one match point in losing his serve at 5-3 but, crucially, he held his next service game and forced three match points in the next, taking the third of them as Djokovic missed an attempted backhand pass.
Moya will feel he has a real shot at upsetting Federer, who needed a big slice of luck in the final set against Ferrer.
Serving at 3-3 and 30-30, the Swiss saw a scooped forehand hit the top of the net and dribble over. It brought a relieved smile from Federer, while Ferrer threw his racket down in frustration on the red clay.
Federer duly held and took a decisive 5-3 lead when he converted a fifth break point in Ferrer's next service game.
"I was lucky there on the net cord," Federer said at a news conference. "That was huge.
"It was a pity that I didn't close out then in the first set, and leave feeling great. But I'm happy with the way I played."
The favourite Rafael Nadal will look to extend his claycourt winning streak to 80 matches when he takes on fifth seed Fernando Gonzalez of Chile. The reward for the winner of that match will be a semi-final against Nicolas Almagro or 16th seed Lleyton Hewitt.
More from rediff