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.
On Monday, Madrid signed Bernd Schuster to a three-year deal to take over as coach for the fired Fabio Capello, who led Madrid to its first Spanish league title in four seasons.