The 16-year-old will only be able to join Real Madrid when he turns 18 in the summer of 2024.
Endrick is considered the next big thing in Brazilian football following some impressive performances for Palmeiras.
Numerous European clubs, among them Barcelona and Chelsea, were also considering moves for the talented teenage forward.
But it is Real Madrid who have won the race to sign him.
A Real Madrid statement read: 'Real Madrid CF, Sociedade Esportiva Palmeiras, Endrick and his family have reached an agreement whereby the player will be able to join Real Madrid when he comes of age in July 2024.
'Until then, Endrick will continue his training at the SE Palmeiras. The player will travel to Madrid in the coming days to visit our club's facilities.'
The Brazilian club's president Leila Pereira said: "We have completed the biggest negotiation in the history of Brazilian football.
"Real Madrid's proposal is compatible with Endrick's enormous talent and corresponds to the sporting and financial goals that we have established since the beginning of negotiations."
Endrick told the Palmeiras website: "I thank Palmeiras, forever the club of my heart, for offering me everything necessary to become what I am today, for helping me to accomplish several of the my dreams and for respecting my desire and that of my family to make another dream come true.
"Until I join Real Madrid, I will continue to dedicate myself as I have always dedicated myself to being able to offer Palmeiras even more on the field."