Obama hailed the deal, a bit more than an hour before a midnight deadline, as "the biggest annual spending cut in history," and House Speaker John Boehner said that over the next decade it would cut government spending by $500 billion.
"This is historic, what we've done," said the third man in the talks, Senate Majority Leader Harry Reid, D-Nev.
They announced the agreement less than an hour before government funding was due to run out. The shutdown would have closed national parks, tax-season help lines and other popular services, though the military would have stayed on duty and other essential efforts such as air traffic control would have continued in effect.
On side issues -- "riders," the negotiators called them -- 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.
Anti-abortion lawmakers did succeed in winning a provision to ban the use of federal or local government funds to pay for abortions in the District of Columbia.
Racing to beat the deadline, lawmakers worked 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, and House members were called into session to follow suit as midnight neared.
The deal came together after six grueling weeks and an outbreak of budget brinksmanship over the past few days as the two sides sought to squeeze every drop of advantage in private talks.