Carvajal, who won the Under-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.
Get the best features, fun and footballing frolics straight to your inbox every week.
Thank you for signing up to Four Four Two. You will receive a verification email shortly.
There was a problem. Please refresh the page and try again.