The Spain Under-21 international will return to his boyhood club after spending just one season with the Bundesliga side.
The right back left Madrid for Leverkusen in the summer of 2012 for a 5 million euro fee, but the Spanish giant insisted on including a buy-back clause when the club let go of the 21-year-old, which the team could activate in the three years after his transfer.
Madrid president Florentino Perez already revealed last week that his side was keen to bring Carvajal back to the Santiago Bernabeu this summer and it has 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.