Federer, 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.
“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 month’s 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.
(Edited by Olawale Alabi)