With oil markets flagging, the world’s two biggest oil exporters agreed on Monday to extend production cuts for several months, sending the price of crude soaring.

Major oil-producing nations have struggled of late to bolster prices, as inventories piled up and crimped the potential for demand. Prices dipped below $44 a barrel this month, their lowest level in more than a year.

Countries like Saudi Arabia and Russia — which are heavily dependent on energy sales to fund national budgets and government services — are trying to manage the markets by promising to reduce production. The move on Monday pushed oil prices up by nearly 3.8 percent, to almost $50 a barrel, the highest level in about three weeks.

The strength in oil prices sent American stocks to new highs. With energy shares surging, the Standard & Poor’s 500 index surpassed its record, closing just above 2,400 points.