"We are delighted to be able to welcome Roger to the tournament this year," event director Manolo Santana told Spanish media on Wednesday.
"It was a pity he missed last year with an ankle injury but the ATP have confirmed he will be with us this time."
World number one Federer has won eight titles this year, including three grand slams.
The Madrid Masters gets underway on Oct. 14.
Tashkent Open: Sania enters second round
Shikha in doubles quarters in Tokyo
Images from the Japan Open
Spotted! Gorgeous Steffi in London
Tursunov: New bad boy of tennis?