Carvajal, who won the U-19 European Championship with Spain last year, had failed to establish himself at the Spanish champions after emerging from the club's youth ranks, which he first joined a decade ago.
No details of a transfer fee were revealed.
"We have managed to strengthen ourselves in the position of right back with this transfer," said Leverkusen boss Wolfgang Holzhaeuser. "Daniel Carvajal is already an outstanding player but still has immense potential due to his young age."
Leverkusen finished in fifth place last season and will compete in the Europa League in the coming campaign.