Spain's Prime Minister Pedro Sanchez visits Morocco on Thursday, after Madrid reversed decades of policy on the Western Sahara to end a year-long diplomatic crisis, a U-turn that has angered Algeria.

The landmark visit will see King Mohammed VI welcome Sanchez as his guest of honour for iftar, the meal where Muslims break their daytime fast during the holy month of Ramadan.

Spain's Prime Minister Pedro Sanchez, pictured on March 23, 2022 during an official visit to Spain's tiny North African enclave of Ceuta
Spain's Prime Minister Pedro Sanchez, pictured on March 23, 2022 during an official visit to Spain's tiny North African enclave of Ceuta LA MONCLOA via AFP / Fernando CALVO

The visit by Sanchez and his foreign minister, Jose Manuel Albares, aims to draw a line under a major stand-off between the two kingdoms.

The crisis began a year ago when Madrid allowed Brahim Ghali, leader of the Polisario Front which seeks independence for the territory of Western Sahara, to be treated for Covid-19 in a Spanish hospital.

Demonstrators in Madrid wave Western Sahara flags during a protest against the Spanish government's support for Morocco's autonomy plan for the territory, on March 26, 2022
Demonstrators in Madrid wave Western Sahara flags during a protest against the Spanish government's support for Morocco's autonomy plan for the territory, on March 26, 2022 AFP / Pierre-Philippe MARCOU

Morocco sees the desert territory as an integral part of the kingdom and a highly sensitive issue of security and national pride.

The area is mostly desert but boasts rich Atlantic fishing waters, phosphate resources and a route to lucrative markets in West Africa.

Map of Western Sahara
Map of Western Sahara AFP / Adrian LEUNG

Morocco fought a bitter war with the Polisario after Spanish colonial forces withdrew in 1975.

Weeks after Ghali's hospitalisation, more than 10,000 migrants surged into Spain's tiny North African enclave of Ceuta as Moroccan border forces looked the other way, in an incident seen as meant to punish Madrid.

Sahrawi women soldiers parade on the 45th anniversary of the proclamation of the Sahrawi Arab Democratic Republic by the Polisario Front, at a refugee camp near the southwestern Algerian city of Tindouf, on February 27, 2021
Sahrawi women soldiers parade on the 45th anniversary of the proclamation of the Sahrawi Arab Democratic Republic by the Polisario Front, at a refugee camp near the southwestern Algerian city of Tindouf, on February 27, 2021 AFP / RYAD KRAMDI

On March 18, Madrid announced a "new stage" in relations with Rabat and said it now backed the North African kingdom's plan for the territory: limited autonomy under Moroccan sovereignty.

Morocco's King Mohammed VI, seen addressing parliament via video link from the Royal Palace in the northeastern city of Fez, on October 8, 2021
Morocco's King Mohammed VI, seen addressing parliament via video link from the Royal Palace in the northeastern city of Fez, on October 8, 2021 Moroccan Royal Palace via AFP

The move was widely seen as a victory for Morocco.

Sahrawi women at the Aousserd refugee camp on the outskirts of the southwestern Algerian city of Tindouf, on February 27, 2021
Sahrawi women at the Aousserd refugee camp on the outskirts of the southwestern Algerian city of Tindouf, on February 27, 2021 AFP / RYAD KRAMDI

It infuriated its regional rival Algeria, which has long backed the Polisario -- and which is a major supplier of natural gas to Spain.

Algiers last month recalled its ambassador from Madrid in protest at the decision, and Algeria's state-owned energy giant Sonatrach warned Friday it could increase the price of its gas sales to the country.

Sanchez, who has also faced a domestic backlash over the move, said last month during a visit to Ceuta that for 10 months, Spain and Morocco had been in "a crisis that could no longer be sustained over time, that we had to solve".

Migrants climb a sea wall in the northern town of Fnideq after attempting to cross the border from Morocco to Spain's North African enclave of Ceuta on May 19, 2021
Migrants climb a sea wall in the northern town of Fnideq after attempting to cross the border from Morocco to Spain's North African enclave of Ceuta on May 19, 2021 AFP / FADEL SENNA

King Mohammed later told Sanchez he wanted to agree an "ambitious road map" on "questions of common interest".

Those questions include irregular migration, the reopening of borders and maritime links, and smuggling around the Spanish enclaves of Ceuta and Melilla, which sit on Morocco's Mediterranean coast.

Moroccan analyst Nabil Driouech said that "economic cooperation is the main driver of relations" between Morocco and Spain, the North African kingdom's main trading partner.

For Spain, the principal aim of mending fences was to guarantee Morocco's cooperation on irregular migration -- which, observers say, Rabat has often used to put pressure on Madrid.

After last year's incident in Ceuta, Madrid had accused Rabat of "blackmail" and Morocco withdrew its ambassador, who has only recently returned.

For Morocco, the question of Western Sahara is the top priority.

"For the moment, the lines will probably not move, but in the long term the independence movement's diplomatic margin of manoeuvre is getting ever more narrow," said Riccardo Fabiani, North Africa project director at the International Crisis Group think tank.

Rabat controls 80 percent of Western Sahara, which the UN considers a "non-autonomous territory".

The Polisario waged a long armed struggle for independence from Morocco before reaching a ceasefire in 1991 on the promise of a referendum on self-determination.

But it has carried out regular, sometimes deadly attacks on Moroccan forces since declaring the ceasefire null and void in November 2020.

Former US president Donald Trump recognised Morocco's sovereignty over the region the following month, and Rabat has since stepped up efforts to have other world powers, particularly in the European Union, follow suit.

Former Spanish ambassador to Morocco Jorge Dezcallar said the latest deal could help establish better ties.

"But I wouldn't be too sure," he added. "It will depend on domestic politics in Morocco", over which Spain has no control.