The European Court of Justice found Oriol Junqueras, who was part of the Catalan Cabinet that went ahead with a banned secession referendum in October 2017, had a right to immunity because he was elected to the European Parliament in May.
The Luxembourg-based Court of Justice said people like Junqueras who are elected as EU lawmakers "enjoy, from the moment the results are declared, the immunity" to travel to and take part in parliamentary sessions.
He was already incarcerated when he was declared to have won the European Parliament seat.
Spain's Supreme Court refused to allow him to leave prison to take an oath to respect the Spanish Constitution, a requirement the country places on politicians before they can take up their seats as EU lawmakers.
The Spanish electoral commission later ruled his seat was vacant and suspended his parliamentary prerogatives.
His case could set a precedent for fugitive former Catalan leader Carles Puigdemont, who was also elected to the European Parliament in May and has been living in exile in Belgium.
Mr Puigdemont is the subject of a Spanish arrest warrant over the independence referendum, but he has also launched an appeal at the ECJ. A court in Brussels ruled earlier this week that it would await the outcome of his European case before deciding whether to extradite Puigdemont.