President Barack Obama and congressional leaders struck a last-minute budget deal on Friday, narrowly averting a government shutdown that would have idled hundreds of thousands of federal workers.

With little time to spare before a midnight deadline for a government closure, Obama's Democrats and opposition Republicans agreed to a bitterly fought compromise plan that would cut $37.8 billion in spending for the rest of the fiscal year.

Congress moved quickly to approve a seven-day stopgap funding measure to keep federal agencies running into next week until the budget agreement can be formally enacted.

A shutdown -- the first in more than 15 years -- would have meant furloughs for much of the federal work force, suspension of key government services and the closing of many national parks and monuments, while potentially undermining the U.S. economic recovery.

But the biggest incentive for a deal may have been the risks that failure would have posed for Obama, his fellow Democrats and the Republicans amid signs of public frustration with the rancorous budget fight in Washington as the 2012 presidential election campaign gathers steam.

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.

The deal for the rest of the fiscal year, which came after days of tense negotiations and brinkmanship, will yield the largest domestic spending cuts in U.S. history, an achievement for Republicans who won control of the House of Representatives last November on a promise to scale back government.

But Obama and the Democrats can take solace in having 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.

Despite the resolution, the political fight raised questions about the ability of Obama and a divided Congress to deal with bigger issues looming down the road, from raising the federal debt ceiling to reining in budget deficits.

Investment firm Goldman Sachs had estimated a government shutdown lasting more than a week could have cost the economy $8 billion in missed federal spending, dragging down growth.

COMPROMISE

Like any worthwhile compromise, 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.

House of Representatives Speaker John Boehner told reporters the sides had come to an agreement that will in fact cut spending and keep our government open.

Obama's aides and U.S. lawmakers had struggled for days to hammer out a deal. Without an agreement, money to operate the federal government for the next six months would have run out at midnight on Friday and agencies such as the Internal Revenue Service would begin a partial shutdown.

The leadership of the world's lone remaining superpower has been consumed for days by the budgetary infighting that could bring large swathes of government to a standstill.

They've got to be laughing at us right now in China, said Senate Foreign Relations Committee Chairman John Kerry. How terrific that the United States of America can't make a decision.

White House Budget Director Jack Lew said Obama was expected to sign the stopgap measure no later than Saturday and told federal agencies to continue their normal operations.

AT ODDS

Democratic and Republican leaders had traded blame for the budget impasse throughout the final day of negotiations.

Senate Democratic Majority Leader Harry Reid said the final issue was a Republican push to give states greater discretion over federal funds earmarked for birth control and women's health clinics. Democrats say that would give Republican governors license to block those funds.

But Boehner said the final stumbling block was spending cuts that Republicans say were needed to rein in budget deficits hitting $1.4 trillion a year.

Fear that a government shutdown could hurt economic growth on the margins pressured the dollar and U.S. Treasury prices on Friday. Longer-term, investors are looking at the last shutdown, in 1995, when predictions of spending cuts made government debt and the dollar more appealing and both rose.

Citigroup noted the budget debate is raising concern among foreign investors about how the U.S. government will deal with the budget deficit, federal debt and the budget talks for the 2012 fiscal year, which begins October 1.

The showdown is the biggest test of leadership for Obama since Republicans -- propelled by Tea Party conservatives -- made big gains and took control of the House last November.

A government shutdown on Obama's watch could have been a negative for him as he seeks re-election. But Boehner was under pressure to stand firm in the talks from Tea Party members who refuse to give ground on spending cuts.

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, Andy Sullivan, Patricia Zengerle, Kim Dixon, Donna Smith, Andrea Shalal-Esa, Jeff Mason and Thomas Ferraro; editing by Will Dunham and Deborah Charles)