In a "Three Stooges" episode, a bungled attempt to find uranium ends with Joe merrily sitting on top of a gushing oil well shouting "Oil's well that ends well." President Enrique Peña Nieto must be feeling the same way.

Some of the reforms he promised have been clumsier than expected, and the economy has almost stalled in his first year in office. But after an all-night session featuring rowdy protests by the left, on Dec. 11 the Senate approved an epoch-making constitutional reform of energy that goes far beyond initial expectations.

The lower house also passed the bill, so Mexico will for the first time in 75 years give up an oil monopoly that many of its people see as a national heirloom. Eventually the reform could bring a wave of much-needed private investment into oil and gas.

Financial markets reacted with a burst of enthusiasm that had been absent for most of the year. The reform was bolder than a bill proposed by Peña in August. It offers private firms a variety of contracts, including licenses to explore and drill for oil, and would allow them to book oil reserves, or the expected revenue streams from them, for financial-reporting purposes, though hydrocarbons below the ground remain state property.

It ends the monopolies of Pemex, the state oil company, and the Federal Electricity Commission, which must now compete against private power generators to supply the grid. It creates a sovereign-wealth fund to invest oil revenues for the long term, though only after covering any shortfall in the government budget.

In a last-minute amendment, it would also strip the powerful and murky oil-workers' union of its five seats on Pemex's 15-member board, which should help make Mexico's largest company more efficient.

The potential benefit from the reform will depend on the strength of secondary legislation early next year that will specify, among other things, what contracts will be offered for which type of oil or gas field, and what royalties and taxes the government will take.

Potential investors will want to see how trustworthy two revamped energy regulators are before committing to big contracts. And the contracts may well require the imprimatur of the Supreme Court. Outside investment is unlikely to flow until 2015 at the earliest.

"Whether you are for or against it, the energy reform is radical," says Guillermo Valdés of GEA, a consultancy. To muster the two-thirds majority needed to approve it, Peña's Institutional Revolutionary Party combined with the conservative National Action Party, which drafted many of the reform's bolder changes. Some analysts think the leftist Party of the Democratic Revolution played its cards badly, missing a chance to turn Mexico into a greener, more innovative energy producer. Instead, it wrapped itself defiantly in the flag while hoping to force a referendum on the energy reform.

Despite Peña's frenetic reform program, Mexicans seem increasingly gloomy. A GEA poll released last week showed that respondents have become more worried about security — especially in their local communities, where many have seen increases in kidnapping and extortion — and have less trust in democracy.

Peña must now hope that the energy reform will give him the last laugh. But if he cannot improve the economy and give Mexicans a sense of security, no one will be laughing with him.

Copyright 2013 The Economist Newspaper Limited, London. All Rights Reserved. Reprinted with permission.