FC Porto defender Pepe will play for AC Milan for five years.
The deal is worth EUR30 million (US$41 million), the Portuguese champions said on the Lisbon Stock Exchange.
The 24-year-old Pepe is the second center back brought to the Santiago Bernabeu stadium, after Germany defender Christoph Metzelder arrived from Borussia Dortmund on Sunday.
The transfer is still dependent on Pepe passing a medical.
Pepe joined Porto in 2004 after four seasons at Madeira Island club Maritimo.
He is the second Brazilian player sold by Porto this summer, with midfielder Anderson leaving for Old Trafford for an undisclosed sum.