The ruling is politically charged as Poland and other eastern European nations fight Gazprom's plans to double the capacity of Nord Stream and bypass legacy gas routes via Poland and Ukraine.
Germany, the main beneficiary of the expanded pipeline known as Nord Stream 2, redoubled its defence of the project on Tuesday, while Poland said it threatened gas supplies to central and eastern Europe.
In a 2016 decision that removed a long-time curb on Russian gas shipments to Europe, the EU executive had lifted a cap on Gazprom's use of Opal.
Since its completion in 2011, Gazprom has only been allowed to use 50 percent of the Opal pipeline under an EU ruling aimed at preventing dominance of the supply infrastructure.
But the European Court of Justice found in favour of a challenge brought by Poland and its state-run gas firm PGNiG, arguing it would lead to a drop in volumes via competing pipelines.
"The court has agreed with our arguments," Polish Energy Minister Krzysztof Tchorzewski said.
The deputy head of PGNiG said the ECJ's decision would prevent Gazprom from completely halting transit via Ukraine, which Kiev fears as its gas-transit contract expires in January.
"Ukraine's negotiating position will improve significantly," PGNiG Chief Executive Piotr Wozniak told reporters.
He said gas volumes being sent through Ukraine would increase by at least 12.5 billion cubic metres, and flows through Nord Stream and Opal should soon fall by a similar amount.
The Opal decision and Gazprom's plan to build Nord Stream 2 play into fears of the Polish conservative government, which sees pacts between its powerful neighbours, Germany and Russia, as an existential threat.
Germany, however, argues the project makes good business sense. "We need it," deputy economy minister, Thomas Bareiss, told an energy conference on Tuesday.
Under current plans, Nord Stream 2 will be owned and operated by Russian gas export monopoly Gazprom, though 50% of the funding is provided by Germany's Uniper and BASF's Wintershall unit, Anglo-Dutch oil major Shell, Austria's OMV and France's Engie.
The ECJ ruled on Tuesday that the 2016 decision is "in breach of the principle of energy solidarity" because it failed to properly assess how to balance Germany's interests against the negative impacts to other EU member states.
"The Commission did not carry out an examination of the impact of the modification of the exemption regime for the OPAL pipeline on Poland's security of supply," the ECJ said in a statement.
"Consequently, the General Court annuls that Commission decision."
A spokeswoman for the European Commission would not comment on whether it would appeal the ruling, which it has the right to do within a little over two months. It said that 2009 rules regulating Opal would now apply.
Gazprom's export arm, Gazprom Export, said it would look into the ruling.
"We are studying legal and commercial consequences of this decision," Gazprom Export said.
The decision will be scrutinised for how it may relate to uncertainty over what rules will govern the operation of a similar pipeline link, Eugal, to carry gas from Nord Stream 2 under the Baltic Sea onshore to Germany.
In light of the court decision, the Commission spokeswoman said the principle of energy solidarity "will need to be assessed explicitly in future exemption decisions."
Bareiss said Germany was close to reaching a decision with the EU on the onshore connection.
Nord Stream 2, meanwhile, has also taken its battle to court. It is challenging new EU rules that it says endanger its business model.
(Reporting by Agnieszka Barteczko and Anna Koper in Warsaw and Alissa de Carbonnel in Brussels and Vera Eckert in Frankfurt; additional reporting by Vladimir Soldatkin in Moscow; editing by Ed Osmond and Bernadette Baum)
By Agnieszka Barteczko and Alissa de Carbonnel