Zidane unexpectedly resigned from the club on May 31 after three seasons on the sideline. His tenure included a La Liga title, three Champions League titles, two UEFA Super Cups, two Club World Cups and two Spanish Super Cups.
Lopetegui, 51, will join the squad after the 2018 World Cup. He is serving as Spain's coach for the tournament in Russia.
"Real Madrid C. F. announce that Julen Lopetegui will be the coach of the first team during the next three seasons," Real Madrid said in a news release.
Lopetegui managed the U19, U20 and U21 Spanish squads, in addition to his post as Spain's senior team boss. He has never lost a game as Spain's manager since assuming the role in 2016.
He previously coached Real Madrid Castilla, after playing for the squad as a goalkeeper. He also played at Madrid, Barcelona, Rayo Vallecano and CD Logrones. Lopetegui began his coach career at Rayo, before joining Real Madrid Castilla.
The Royal Spanish Football Federation released a statement regarding Lopetegui's relinquishment of the role after the World Cup.
"The RFEF will receive the payment of the release clause so that the current national coach can sign for Real Madrid," the statement said.
"The RFEF asks for the maximum respect to maintain normality in the national team camp ahead of our debut in the tournament against Portugal."
The RFEF announced in May that Lopetegui would coach Spain until 2020.
Real Madrid battles Manchester United in the International Champions Cup at 8 p.m. on July 31 at Hard Rock Stadium in Miami.