VIENNA — After years of trying fruitlessly to prop up energy markets, OPEC on Wednesday finally reached a consensus on production cuts, sending oil prices soaring. The problem is, the euphoria in the markets may not last.

With prices still at less than half the levels of two years ago, the Organization of the Petroleum Exporting Countries agreed this fall to lower collective production. But it could not figure out how to spread the cuts among the countries.

The path to consensus has been complicated by Saudi Arabia and Iran, whose longstanding mutual enmity encompasses religious, political and economic competition. When it comes to oil, Saudi Arabia, OPEC’s top producer, has fought to maintain its market share, while Iran has worked to protect its nascent comeback as a power broker in the cartel, a role it lost in recent years under Western sanctions tied to its nuclear program.

They overcame their differences on Wednesday, with OPEC deciding to cut production next year by about 4.5 percent, or 1.2 million barrels a day. It will be the first cut in eight years.