Militao was previously signed from Sao Paulo where he played in the 2017/18 season.
The defender, who will join in the summer, is 21 years old and is a full worldwide with Brazil.
The 21-year-old will provide competition for veteran defender Sergio Ramos and World Cup victor Raphael Varane next season, as Madrid look to refresh an ageing squad under Zidane's leadership.
The centre-back is the Spanish giants' first signing since Zinedine Zidane returned to the club as coach for a second spell this week.
The 21-year-old central defender is comfortable at right-back and in midfield and has been courted by a host of Europe's elite but Los Blancos have acted swiftly to secure the signature of the highly-rated Brazilian talent.
Militao only arrived in European football during the summer of 2018.
He will have seen out less than 12 months of that agreement before heading to Madrid.
A man who made his senior worldwide debut for Brazil in September 2018 will join a defensive unit in Spain which has been far from watertight this season.
He has enjoyed considerable success in that role before and will be hoping to see Militao help to deliver more once welcomed into the Real fold.