"After feeling pain in his right ankle and a medical checkup, the Real Madrid player Pepe was operated on this morning in Porto," the club said on their website.
They did not say how long the Portuguese international would be out of action but local media reported he would be sidelined for about a month.
Real have been hit by several injuries in defence in recent weeks, including fullbacks Fabio Coentrao and Marcelo.
Pepe's regular partner at centre-back, Sergio Ramos, is suspended for Sunday's game at home to Real Sociedad and French teenager Raphael Varane could partner the little-used Ricardo Carvalho in the middle of a makeshift defence as Real seek to get their La Liga campaign back on track after a poor run.
They are third after 17 matches, seven points behind second-placed Atletico Madrid and 16 adrift of unbeaten leaders Barcelona.
Get the best features, fun and footballing frolics straight to your inbox every week.
Thank you for signing up to Four Four Two. You will receive a verification email shortly.
There was a problem. Please refresh the page and try again.