“The Madrid Open can count upon the presence of Roger Federer,” said a statement on the tournament's website.
“Federer is one of the best tennis players in history and Madrid is a special place for him,” said tournament director Manolo Santana.
“We are delighted to be able to once again count on his presence at the Caja Magica.”
The world number three had initially planned to skip the fourth Masters series event of the season to reserve his energy for the three remaining Grand Slams and Rio Olympics later in the year.
However, the Swiss's plans have changed having missed nearly three months of the season due to knee surgery, including the Masters series events in Indian Wells and Miami.
Federer will be joined in the Spanish capital by world number one Novak Djokovic, defending champion Andy Murray and Rafael Nadal fresh from his wins in Monte Carlo and Barcelona.
The 34-year-old is a three-time winner in Madrid having claimed the title in 2006, 2009 and 2012.