The centre-back is the Spanish giants' first signing since Zinedine Zidane returned to the club as coach for a second spell this week.
Real said Militao would join on a four-year deal until 2025. Meanwhile, in a statement to the Portuguese stock exchange, Porto said Real had agreed to pay the player's release clause of 50 million euros ($56.5m), "effective at the end of the season".
That will mean a significant profit for Porto on a player they signed last year from Sao Paulo in his homeland.
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.
He is likely to be followed by several more new arrivals before the start of next season as Real prepare to rebuild following what has been a disappointing campaign.
Zidane replaced Santiago Solari on Tuesday on a contract until 2022 with Madrid out of the Champions League and Copa del Rey, and lying third in La Liga, 12 points adrift of leaders Barcelona with 11 matches left.