The Spain Under-21 international will return to his boyhood club after spending one very impressive season with the Bundesliga side
The right-back left Madrid for Leverkusen in the summer of 2012 for a €5m (£4.26m) fee, but the Spanish giants insisted on including a buy-back clause when they let go of the 21-year-old, which they could activate in the three years after his transfer.
Madrid president Florentino Perez already revealed last week that they were keen to bring Carvajal back to the Santiago Bernabeu this summer and they have now wrapped up the transfer of the defender.
"We are sad that Daniel will no longer be a Leverkusen player, but we knew right from the start that Madrid would acknowledge his potential at some point," Leverkusen sporting director Rudi Voller stated.
"Madrid's decision to re-sign him this summer does not come as a surprise to us after his performances this term."
Carvajal made 32 Bundesliga appearances for Leverkusen in 2012-13, scoring once in the process.