The Polisario Front's Brahim Ghali has been receiving treatment for Covid-19 in Spain since April 18. He is under investigation for crimes including genocide, terrorism and torture, based on accusations by blogger Fadel Breica and rights organisation ASADEH.
The court however ruled that there was no immediate danger of Ghali fleeing or destroying evidence. The accusations were also not supported by substantive evidence he had committed a crime, it found.
The ruling is sure to anger Morocco, which claims large parts of Western Sahara. Polisario is fighting for the territory's independence.
The government in Rabat is already furious that Ghali is not being prosecuted in Spain. The "core of the crisis" is Spain's "ulterior motives that are hostile" to the question of Western Sahara, the Moroccan Foreign Ministry said in a statement on Monday.
Spain, which ruled Western Sahara as a colonial power until 1975, does not recognize Morocco's territorial claims.
The relationship between the two countries has recently deteriorated.
Two weeks ago, Morocco loosened border controls at Spain's North African exclave Ceuta, leading to more than 9,000 migrants reaching the territory. Spain afterwards accused Morocco of "blackmail."