The diplomatic rift between Spain and Morocco widened on Tuesday night when the leader of the Western Sahara independence movement left Spanish territory after a Madrid judge ruled he was free to go.

The presence in Spain of Brahim Ghali, head of the Polisario Front, which has fought for Western Sahara’s independence from Morocco since the 1970s, precipitated a migrant and border crisis with Rabat last month.

Morocco made clear it wanted Ghali, whom it denounces as a terrorist, to be incarcerated. But on Tuesday, high court judge Santiago Pedraz declined to place Ghali in detention or to order him to surrender his passport, saying allegations that he was involved in torture and disappearances were not corroborated by plaintiffs in the war crimes case against him.

Spanish officials said late on Wednesday night that Ghali was subsequently discharged from the hospital where he had been treated for Covid-19 and had left Spain on a civilian flight from Pamplona airport.

Earlier in the day, Ghali’s lawyer, speaking after the Western Saharan leader had given evidence via video link from hospital, said the claims against his client were a politically motivated attack on the Western Sahara cause.

Rabat claims sovereignty over the desert region, which is roughly the size of the UK, and a 30-year ceasefire with Polisario broke down at the end of last year.

The Moroccan government sees Ghali as one of its principal enemies. His arrival in Spain for treatment in April was followed by a migrant crisis, when around 10,000 people crossed over from Morocco to the Spanish north African enclave of Ceuta in 48 hours.

Although Spanish officials say Moroccan border guards allowed and encouraged the crossings, Rabat denies that the incursion was in any way connected with Ghali’s case and has readmitted around 8,500 people.

But the Moroccan foreign affairs ministry declared on Monday that the case “exposed the hostile attitudes and harmful strategies of Spain towards the question of the Moroccan Sahara [and] revealed the collusion of our northern neighbour with the Kingdom’s adversaries to undermine the territorial integrity of Morocco”.

The ministry drew a parallel between the Western Sahara dispute and the controversy over Catalan independence, asserting that Rabat had consistently supported Spain’s territorial integrity. It added: “How in this context can Morocco trust Spain again? How does one know that Spain will not plot again with the enemies of the Kingdom?”

Algeria, Morocco’s neighbour and longstanding rival, is Polisario’s main backer, hosting the group in its territory and providing its leaders with passports to travel on.

In response to Rabat’s statement, María Jesús Montero, Spain’s ministerial spokesperson, said on Tuesday that it was “not acceptable for the Moroccan government to defy [Spain’s] border and territorial integrity because of differences in this dispute”. She emphasised Madrid’s position on the Western Sahara was in line with UN resolutions.

On Tuesday, the European Commission said it expected that the “deep relationship” between the two countries would allow them to reduce tensions.

Riccardo Fabiani, north Africa director at the International Crisis Group, a conflict-resolution organisation, also expressed doubts over how much Morocco would escalate the rift with Spain. “I doubt Morocco will want another problem with Spain,” he said, noting that the country’s isolation from Europe could increase.

Ties between Morocco and Germany are already under strain and the kingdom recalled its ambassador from Berlin for consultation last month — also after tensions over Western Sahara.

After the Trump administration recognised Moroccan sovereignty over the region in December — in return for Morocco’s normalisation of ties with Israel — Germany called for a closed-door UN Security Council meeting to debate the status of the region.