Judges in Luxembourg decided that the UK can stay in the EU "under terms that are unchanged" if it decides to change its mind on Brexit "through a democratic process".
It comes on the eve of a crucial vote in the House of Commons on Theresa May's Brexit deal. Scottish politicians who brought the case said "a bright light has switched on above an 'EXIT' sign" that meant a second EU referendum was "closer than ever before".
Issuing its judgement, the EU’s top court said that “when a member state has notified the European Council of its intention to withdraw from the European Union, as the UK has done, that member state is free to revoke unilaterally that notification.”
Downing Street has said it will not revoke Article 50 under any circumstances. Judges at the ECJ went further than a legal opinion issued by a top law officer at the court last week, in confirming the UK can retain its existing membership terms if it revokes Article 50 before the official Brexit day of 29 March, 2019.
The ruling was welcomed by the cross-party group behind the case, which began the action at the Court of Session in Edinburgh, supported by campaigners at the Good Law Project. Andy Wightman, the Green MSP who was the lead petitioner, said judges in Luxembourg had been "clear and unambiguous" in backing their claim.
The Scottish Government's Constitutional Relations Secretary Michael Russell also welcomed the "hugely important decision".
“People in Scotland overwhelmingly voted to remain in the EU and that continues to be the best option for Scotland and the UK as a whole," Mr Russell said.
“This judgment exposes as false the idea that the only choice is between bad deal negotiated by the UK Government or the disaster of no deal.
“We now know, thanks to the efforts of Scotland’s parliamentarians, that remaining in the EU is still on the table.”
SNP MP Joanna Cherry said she was “delighted that we now know definitively that there is an option to stay in the EU”.
Ms Cherry said: “The UK government has ignored Scotland’s vote to remain and all compromises suggested by the Scottish Government.
“They also fought us every inch of the way in this case, so it’s a particularly sweet irony that Scottish parliamentarians and the Scottish courts have provided this lifeline to the UK parliament at this moment of crisis.”
SNP MP Alyn Smith described the ruling as “dynamite.”
"Bringing the case was a risk but it has worked better than we could have hoped for,” Mr Smith said.
"The timing is sublime. As colleagues in the House of Commons consider Mrs May’s disastrous deal we now have a roadmap out of this Brexit shambles.
“A bright light has switched on above an 'EXIT' sign.”
Liberal Democrat MP Tom Brake said the ruling meant the UK was "now closer than ever before" to a new referendum on EU membership.
"For the sake of people's livelihoods, the Prime Minister must end the uncertainty and rule out a no-deal," Mr Brake said.
"It is clear any Brexit will make people poorer and reduce the UK's standing in the world. MPs should not only vote down Theresa May's deal, but back a People's Vote with the option to remain in the EU."
Ms Stihler said: “When MPs vote on Theresa May’s Brexit deal, they now know they can ultimately choose to stop Brexit and keep the best deal we currently have as a full member of the EU.
“I am increasingly confident that is what will happen if MPs agree to a People’s Vote, which is what the country now urgently needs.”