President Barack Obama and congressional leaders reached a historic, last-minute agreement just before a midnight deadline to slash about $38 billion (£23bn) in federal spending and avert the first federal government shutdown in 15 years.
Mr Obama hailed the deal as "the biggest annual spending cut in history".
John Boehner, the Republican speaker of the House of Representatives, said that over the next decade it would cut government spending by $500 billion (£300bn), and won an ovation from his rank and file - conservative tea party adherents among them.
Amid the biggest clash yet between Democrats and the resurgent Republicans who control the House, Mr Obama had warned that a shutdown would damage the economy's recovery by putting an estimated 800,000 government employees out of work.
The political stakes of a shutdown were huge ahead of next year's presidential and congressional elections.
During the last government shutdown during Bill Clinton's presidency, Republicans got most of the blame in - but there was no assurance that would have happened again.
Since taking control of the House in January, Republicans have vowed to slash what they described as out-of-control spending and curb the federal deficit.
Democrats accused Republicans of wanting to cut vital government services and pushing a social agenda, while Republicans said Democrats were not serious about cutting spending.
The deal came together after six gruelling weeks and an outbreak of budget brinkmanship over the past few days as the two sides sought to squeeze every drop of advantage in private talks.
"This is historic, what we've done," agreed Democratic Senate Majority Leader Harry Reid, the third man involved in negotiations that ratified a new era of divided government.
Mr Obama, Mr Boehner and Mr Reid announced the agreement less than an hour before government funding was due to run out.
The shutdown would have closed national parks and other popular services, though the military would have stayed on duty and other essential operations such as air traffic control would have continued.
The Democrats and the White House rebuffed numerous Republican attempts to curtail the reach of the Environmental Protection Agency and sidetracked their demand to deny federal funds to Planned Parenthood, which provides family planning and other medical services.
Anti-abortion politicians did succeed in winning a provision to ban the use of government funds to pay for abortions in the Washington capital district.
Politicians raced to pass an interim measure to prevent a shutdown, however brief, and keep the federal machinery running for the next several days.
The Senate acted within minutes. The House worked past midnight, so the federal government was to be technically unfunded for a short period of time, but there would be little - if any - practical impact
Mr Reid, Mr Obama and Mr Boehner all agreed a shutdown posed risks to an economy still recovering from the worst recession in decades.
But there were disagreements aplenty among the principal players in an early test of divided government - Mr Obama in the White House, fellow Democrats in control in the Senate and a new Republican majority in the House bolstered by conservative tea party-affiliated freshmen.