Daniel Galvan, 63, was among 48 Spanish prisoners pardoned by King Mohammed VI and freed from jail following a visit in mid-July to Morocco by Spain's King Juan Carlos.
Mohammed revoked the pardon in August after violent protests in Morocco over Galvan's release, with officials saying it had been a mistake. Galvan had travelled in the meantime to Spain, where he was re-arrested.
The National Court in Madrid said in a ruling on Monday that the existing treaties between Spain and Morocco did not allow for one of the countries to extradite its own nationals to the other.
There were therefore "no grounds to hand the Spanish national Daniel Galvan Vina to Morocco under the international arrest warrant" issued by a Moroccan judge on August 5, it said.
It ruled that he should serve his 30-year sentence in prison in Spain.
Galvan was born in Iraq but acquired Spanish citizenship after he married a Spanish woman whom he has since divorced.