The 37-year-old Swiss, who was making a return to the European clay court season after two years away, won back-to-back matches against Joao Sousa and Borna Coric to reach the quarter-finals on Thursday.
Federer blamed an injury to his right leg, which allows Stefanos Tsitsipas - his conqueror at the Australian Open in January - to progress to the semi-finals.
"Unfortunately, King Roger had to withdraw from the tournament due to an injury to his right leg," the tournament organisers wrote on Twitter.
"We wish him a fast recovery. Thank you for the incredible emotions."
The injury casts doubts over his preparations for this months French Open, with only five matches on clay under his belt heading into the second Grand Slam of the year.
Following his quarter-final exit at the Australian Open, the 20-times Grand Slam champion won his 100th career singles title in Dubai and added another crown in Miami.
Federer, bidding for only the second French Open title of his career, will look to regain fitness ahead of the tournament starting in Paris on May 2