Spain’s squad have been plunged into crisis after the coach Julen Lopetegui was sacked just two days before their opening World Cup game against Portugal in Sochi, with the director of football, Fernando Hierro, named as his replacement.

The president of the Spanish Football Federation, Luis Rubiales, insisted that he had little choice but to terminate Lopetegui’s contract as a matter of principle after the coach negotiated with Real Madrid behind the federation’s back. The sacking comes in response to the news, released on Tuesday afternoon, that Lopetegui had agreed to take over at Real Madrid after the World Cup ends.

Julen Lopetegui sacked by Spain on eve of World Cup – live! Read more

That came as a huge surprise. Neither the players nor Hierro nor the president knew of the decision until moments before it was made public. Rubiales said that he had received a phone call from Madrid five minutes before they issued a statement announcing the appointment. He flew from Moscow to Krasnodar on Tuesday night, leaving the Fifa congress to head to the national team’s HQ to discuss how best to resolve the situation.

Just after 1pm on Wednesday afternoon, after discussions with the players, he announced the decision to sack the coach, describing it as an “obligation” on a “sad day”.

The announcement that Hierro would be his replacement followed three hours later. “It is a nice challenge. I could not say no: I would not have forgiven myself,” Hierro said.

“We are in an extremely difficult situation; this is very, very hard,” Rubiales had said earlier. He and Lopetegui were due to appear before the media the following morning but that was put back and when Rubiales eventually appeared, an hour and a half later than scheduled, he did so alone. Another planned press conference with Lopetegui, subsequently pencilled in to follow that of the federation president, was cancelled.

Facebook Twitter Pinterest Julen Lopetegui leads a training session in Krasnodar on Tuesday before his departure was confirmed. Photograph: Javier Etxezarreta/EPA

Rubiales had spent the morning talking to players, led by Sergio Ramos, who argued for the coach to continue. Ultimately, though, he chose to sack Lopetegui with immediate effect.

“There are decisions that we are obliged to take based on an understanding of how you should behave and on ethics,” he said. He added that it was legitimate for Lopetegui to negotiate with Real Madrid but not for the federation to find out just five minutes before a formal announcement was made, a matter of hours before the beginning of the tournament.

Although he sought not to point the finger directly at Madrid or Lopetegui, insisting that “if it was up to the coach I am sure he would have done things differently”, Rubiales was furious.

He has been the president of the federation for less than a month and has already been forced to manage his first great crisis. He said that there was no negotiation with Real Madrid and that his request that the announcement be delayed was ignored, with the deal made public before he could respond. He attempted to contact Lopetegui to speak to him but was unable to do so as the coach was telling his news to the players. Madrid released the statement “five minutes” later, he said.

Sign up for the World Cup Fiver, the Guardian’s free and funny daily football email.

“The national team belongs to everyone and we have to send a clear message to all employees at the federation that there is a way of behaving, that there are ethics,” he said. “Julen has worked phenomenally well; I have nothing at all to complain about there. But the way that this has happened obliged me to take this decision. This is a very hard day and now we have decisions to make just two days before the World Cup starts.”

It was later confirmed that Hierro, who won 89 caps for Spain, would take charge of the team for the duration of the World Cup. “I am grateful that Fernando said yes,” Rubiales said. “This [episode] has not left a good taste but we will overcome this.”

Hierro said: “We’ve come to fight for the World Cup. The team is excited. We have a great opportunity and that should be the focus. The message from the team was fantastic. If I were not convinced of our aspirations, I would not be here. The key will be to change as little as possible.”

The Juventus manager Max Allegri, meanwhile, has claimed he turned down an offer from Real Madrid to take over from Zinedine Zidane. Speaking to Sky Italia, he said: “Real Madrid and Chelsea? Let’s say I said yes to Juventus. I spoke with the president and it was right for me to stay at Juventus. It was a decision I had already made.”

“I decided to stay at Juve and I respected what I said, I thank the president of Real who gave me the chance to go. Chelsea? The decision to remain at Juventus had been taken and it was right to confirm the word given to Juventus.”