President Barack Obama and congressional leaders struck a last-minute budget deal on Friday to narrowly avert a government shutdown that would have hit the economy and idled hundreds of thousands of workers.

With a little over an hour to spare before a midnight deadline, Obama's Democrats and opposition Republicans agreed to a bitterly fought compromise plan that will cut about $38 billion in spending for the rest of the fiscal year.

Congress then quickly approved a stopgap funding measure to keep the federal government running into next week until the budget agreement can be formally approved.

A shutdown -- the first in more than 15 years -- would have weakened the U.S. economic recovery, forced furloughs for some 800,000 federal employees, closed national parks and monuments and even delayed paychecks for troops in Afghanistan and Iraq.

But the biggest incentive for a deal may have been the risks that failure would have posed for Obama, his Democrats and the Republicans just as the 2012 presidential election campaign gathers steam.

Public frustration over the budget fight had surged as Democrats and Republicans traded blame and a shutdown loomed.

Tomorrow, I'm pleased to announce that the Washington Monument as well as the entire federal government will be open for business, Obama said in a late-night appearance at the White House shortly after the agreement was reached.

It provides the largest spending cuts in U.S. history, a victory for Republicans who won control of the House of Representatives in November on promises to scale back government.

House of Representatives Speaker John Boehner, who came under intense pressure from Tea Party conservatives inside his own Republican Party to take an even tougher stance, said the deal clears the way for bigger spending cuts in coming years.

But Obama and the Democrats were able to beat back a Republican effort to block birth control funding to the Planned Parenthood family planning organization, because it also provides abortions -- though not with public money.

Both sides had to make tough decisions and give ground on issues that were important to them, Obama said. Some of the cuts we agreed to will be painful.

DIVIDED CONGRESS

Still, the fight did little to improve the view Americans have of their political leaders, and raised concerns about the ability of Obama and a divided Congress to deal with bigger issues, from raising the federal debt ceiling to reining in budget deficits.

Even some lawmakers said the bitter feuding had sent a bad message to the rest of the world.

They've got to be laughing at us right now in China, said Senate Foreign Relations Committee Chairman John Kerry.

Fears that a government shutdown would hurt economic growth had pressured the dollar and U.S. Treasury prices on Friday.

All sides agreed the debate had been long and painful.

It has been a grueling process. We didn't do it at this late hour for drama. We did it because it has been very hard to arrive at this point, said Senate Majority Leader Harry Reid, a Democrat.

After the deal was finally reached, White House Budget Director Jack Lew told federal agencies to continue their normal operations.

But there will almost certainly be a much bigger showdown over the budget for the next fiscal year, which begins on October 1.

Republicans are already pledging to slash taxes and overhaul Medicaid and Medicare, government-run health programs for the poor and elderly. The Democratic-controlled Senate is likely to flatly reject those plans.

A government shutdown could have been a negative for Obama as he seeks re-election. But there were significant risks for Republican leaders, too, especially if they were seen as being under the thumb of Tea Party radicals.

The budget battle has dominated Obama's agenda even as he struggles to balance Americans' chief concerns -- jobs and the economy -- with foreign policy challenges topped by Middle East turmoil and U.S. military involvement in the Libyan conflict.

(Additional reporting by Alister Bull, Patricia Zengerle, Richard Cowan, Matt Spetalnick, Kim Dixon, Donna Smith, Andrea Shalal-Esa and Thomas Ferraro; Editing by Kieran Murray)