“Real Madrid announce that Julen Lopetegui will be the coach of the first team during the next three seasons,” the Champions League winners said in a statement, adding that the 51-year-old “will join the club after Spain's participation in the World Cup”.
Lopetegui will replace Zinedine Zidane as coach at the Santiago Bernabeu, after the Frenchman quit on May 31 in the wake of their Champions League final win over Liverpool in Kiev.
The news comes as a jolt for Spain, just three days before their opening game at the Russian World Cup against Iberian neighbours Portugal in Sochi.
Lopetegui and the Spain squad are currently at their training base in the southern Russian city of Krasnodar.
The Spanish Football Federation (RFEF) confirmed that Madrid will pay compensation to get Lopetegui out of the two years he has remaining on his contract with the national team.
He only signed an extension to his deal until 2020 last month.
“The RFEF will receive payment of the release clause so that the current national coach can join Real Madrid,” read a statement, adding that Lopetegui would appear alongside federation president Luis Rubiales at a press conference at Spain's Russian training base on Wednesday at 11:30am (0830 GMT).
Spain are among the favourites to win the World Cup after an almost flawless qualifying campaign, and he will be hoping that the announcement does not overly unsettle his squad just before the competition gets under way.
Lopetegui's national squad includes six Real players in Dani Carvajal, Sergio Ramos, Nacho Fernandez, Marco Asensio, Isco and Lucas Vazquez.
“The RFEF requests the maximum respect in order to maintain normality in the national team camp ahead of our debut in the tournament against Portugal,” added the federation's statement.
Lopetegui was appointed as Spain coach in the wake of their poor showing at Euro 2016 in France when they were eliminated in the last 16, losing 2-0 to Italy.
Zidane quit Real after a remarkable two and a half years in charge during which he led the club to three consecutive Champions League titles, as well as one La Liga crown.
However, Real finished third in La Liga in the season just finished, 17 points behind champions Barcelona, and Zidane had admitted that he was unsure of his ability to oversee more success in the future.
His decision to quit as Madrid coach suddenly left the Spanish giants in limbo. Tottenham Hotspur manager Mauricio Pochettino had initially emerged as the leading candidate to replace the Frenchman but will now stay in the Premier League.
As a player, Basque-born Lopetegui was a goalkeeper on Real's books early in his career, and he returned for a spell in charge of their second team a decade ago.
He also briefly played for Barcelona and later led Spain's under-21 side to the European Championship title in 2013.
He went on to coach Porto before succeeding Vicente del Bosque, who had overseen Spain's triumphs at the 2010 World Cup and Euro 2012 but had also been in charge for the disappointing group-stage exit at the 2014 World Cup in Brazil.
La Roja cruised through qualifying for this year's World Cup, with nine wins and a draw in 10 games, scoring 36 goals and conceding three.
Indeed, Spain are still unbeaten under the Basque after 20 matches including friendlies.