The measure was deadlocked for months because of a split between the European Parliament and EU states, which have joint say.
The countries that were against a CDS ban agreed to it after the parliament said they could opt out if the curb was damaging their government debt market.
"It is a very ambitious accord which strengthens financial stability and strengthens the single market for financial services," Michel Barnier, the EU's financial services chief, told a news conference to announce the deal.
The law, which also includes conditions and reporting requirements on shortselling shares, will take effect from Nov 1, 2012 on new contracts.
The European Securities and Markets Authority, a pan-EU supervisory body, will play a central role in determining whether a market is being damaged to trigger anopt out.
"The criteria for this opt-out will be European, will be looked at on a European basis," Pascal Canfin, the French Green Party member who is sponsoring the law in the parliament, told the news conference.
A national supervisor that wants a national opt-out would have to provide ESMA with its "objective reasons" and analysis, and a decision would be reached within 24 hours.
"ESMA won't have any power to impose its decision but there will be political pressure and there will be judicial pressure if the framework is not respected," Canfin said.
Britain, the EU's main CDS trading centre, has been opposed to such bans but was out-voted.
"I'm reassured that member states will have the ability to opt out of the ban, if they see signals that sovereign debt markets are distressed," said Syed Kamall, a centre-right member of the European Parliament who represents London.
CDS and Greece
Policymakers accused hedge funds last year of using CDS contracts to bet on a Greek default, a practice they said made it more expensive for the EU to rescue Greece.
This prompted French President Nicolas Sarkozy and German Chancellor Angela Merkel to call for the draft law.
But a study for the European Parliament said a ban on naked CDS would have "detrimental effects on liquidity and the price discovery process of credit risk."
Hedge funds say the CDS market is too small to manipulate the far-bigger sovereign debt market and that government bond prices have tumbled in Greece and elsewhere in the euro zone because of investor worries over the level of public debt.
"We have previously expressed our concerns about the impact of a ban on uncovered sovereign CDS," said Andrew Baker, chief executive of hedge fund lobby AIMA.
"It could not only reduce liquidity and increase volatility in debt markets, but also increase government borrowing costs and reduce real economy investments in EU member states."
The measure aims to avoid a repeat of confusing unilateral national curbs on short-selling in financial shares in 2008 after the collapse of U.S. bank Lehman Brothers.
Italy, Spain, France, Belgium and Greece reintroduced short-selling curbs this summer, while other EU states like Britain refused to join in.
Such curbs are disputed by exchanges and some academics, and they failed this summer to stop a rout in French banking shares as the euro zone crisis sent investors scurrying.
ESMA would have powers to override national supervisors and impose temporary pan-EU share short-selling bans in times of market turmoil.
The new law will also impose reporting requirements on short positions in shares and sovereign debt.
Shorting stocks would only be allowed if prior arrangements have been made to borrow the stock to ensure prompt settlement of trades or that there is reasonable certainty the stock will be available.
EurActiv with Reuters