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.
President Barack Obama says a last-minute deal with congressional leaders to avert a government shutdown happened because "Americans of different beliefs came together."
House Speaker John Boehner informed the GOP rank and file of the accord, reached in grueling negotiations over several weeks, an official said.
"We have an agreement," concurred a spokesman for Senate Majority Leader Harry Reid, Jon Summers.
Because drafting and then passing the broader legislation could take days, congressional leaders raced to approve a stopgap measure to prevent the onset of the first shutdown in 15 years, due to begin at midnight. Officials said it would keep the government in funds through the middle of next week.
Boehner told reporters just before 11 p.m. EDT that the House would continue working.
Republicans said the deal called for $39 billion in spending cuts, a measure that one official said Boehner told his rank and file marked the "largest real-dollar spending cut in American history."
Over a decade, the agreement would cut more than $500 billion from the federal budget, Boehner added, according to a participant in the meeting.
The agreement marked an extraordinary reach across party lines and the first test of a new era of divided government that includes Obama in the White House, control of the Senate by fellow Democrats and a tea party-flavored Republican majority in the House.