Militao, 21, will move to the Bernabeu in June on a six-year deal, Madrid announced in a statement.
Militao, who can also play at right-back and has been capped once by Brazil, reinforces a Real defence that has often been diminished by injuries this season.
In another statement on their website, the club said: "Eder Gabriel Militao will join the club next season and is already a full Brazil worldwide". Militao was named as the best defender in the competition for five consecutive matches.
The Brazilian will arrive in the summer and means that Los Blancos now have four centre-backs they can rely on.
Militao has scored three times in 34 appearances for Porto during the 2018-19 campaign.
The centre-back, who started his professional career with Sao Paulo before moving to Porto in August 2018, has been linked with a number of European clubs in recent months.
"His superb performances have led to him making his full Brazil global debut in a clash with El Salvador in September 2018".
"He cut his teeth in the Sao Paulo academy, which he joined at 13, before going on to win the U-20 Copa de Brasil in 2016".