Spain has assured Algeria that it will not send its imported supplies of natural gas to Morocco or "a third destination".
Algerian authorities have threatened to suspend its gas exports to Spain, amid skyrocketing prices driven by Russia’s war in Ukraine.
Spain has been in talks with Morocco about helping its North African neighbour boost its gas supplies by allowing Rabat to use its processing facilities and an existing pipeline that crosses the Strait of Gibraltar.
Algeria and Morocco are however in the midst of diplomatic tensions and gas exports from Algiers were switched off last year.
Spain also imports natural gas from Algeria, and said in February it would help Morocco "guarantee its energy security".
In a statement late on Wednesday, Algeria’s energy ministry warned that gas supplies it sends to Spain could be suspended if that gas is then diverted elsewhere.
But Spain's government has reiterated that “in no case will the gas acquired by Morocco come from Algeria”.
"The activation of this mechanism has been discussed with Algeria in recent months," the Ministry for the Ecological Transition added in a statement.
Until last October, part of the supplies of Algerian gas to Spain came via the Maghreb Europe Pipeline through Morocco.
Morocco had also received some supplies to produce 10% of its electricity before Algeria decided to end the 25-year gas distribution agreement last August.
Now, Algerian gas now reaches Spain via a longer pipeline direct to Almería on the southeastern Spanish coast.
As European countries seek alternatives to Russian gas amid the war in Ukraine, supplies from Algeria have taken on added importance.
Spain is a leader in wind and solar power but continues to rely heavily on energy imports — with Algeria providing more than a quarter of its natural gas.
But the diplomatic freeze between Morocco and Algeria has forced Spain into a delicate balancing act.
The feud between Rabat and Algiers centres on the disputed region of Western Sahara, a former Spanish colony in North Africa that was annexed by Morocco in 1976.
Algeria backs the Polisario Front independence movement in Western Sahara.
In March, it recalled its ambassador to Madrid in protest when Spain backed a Moroccan plan to give more autonomy to the contested territory.