WASHINGTON -- Up against a deadline, Congress passed and President Barack Obama signed legislation late Wednesday night to avoid a threatened national default and end the 16-day partial government shutdown, the culmination of an epic political drama that placed the U.S. economy at risk.

The Senate voted first, 81-18, at midevening. That cleared the way for a final 285-144 vote in the Republican-controlled House about two hours later on the legislation, which hewed strictly to the terms Obama laid down when the twin crises erupted more than three weeks ago.

The legislation would permit the Treasury to borrow normally through Feb. 7, or perhaps a month longer, and fund the government through Jan. 15. More than 2 million federal workers would be paid -- those who had remained on the job and those who had been furloughed.

After the Senate approved the measure, Obama hailed the vote and said he would sign it immediately after it reached his desk. "We'll begin reopening our government immediately, and we can begin to lift this cloud of uncertainty from our businesses and the American people."

Later, in the House, Rep. Harold Rogers, R-Ky., said, "After two long weeks, it is time to end this government shutdown. It's time to take the threat of default off the table. It's time to restore some sanity to this place."

The stock market surged higher at the prospect of an end to the crisis that also had threatened to shake confidence in the U.S. economy overseas.

Republicans conceded defeat after a long struggle. "We fought the good fight. We just didn't win," conceded House Speaker John Boehner as lawmakers lined up to vote on a bill that includes nothing for GOP lawmakers who had demanded to eradicate or scale back Obama's signature health care overhaul.

"The compromise we reached will provide our economy with the stability it desperately needs," said Senate Majority Leader Harry Reid, declaring that the nation "came to the brink of disaster" before sealing an agreement.

Senate Republican leader Mitch McConnell, who negotiated the deal with Reid, emphasized that it preserved a round of spending cuts negotiated two years ago with Obama and Democrats. As a result, he said, "Government spending has declined for two years in a row" for the first time since the Korean War. "And we're not going back on this agreement," he added.

Only a temporary truce, the measure set a time frame of early this winter for the next likely clash between Obama and the Republicans over spending and borrowing. But for now, government was lurching back to life. Within moments of the House's vote, Sylvia Mathews Burwell, director of the Office of Management and Budget, issued a statement saying "employees should expect to return to work in the morning."

After weeks of gridlock, the measure had support from the White House, most if not all Democrats in Congress and many Republicans fearful of the economic impact of a default.

Boehner and the rest of the top GOP leadership told their rank and file in advance they would vote for the measure. In the end, Republicans split 144 against and 87 in favor. All 198 voting Democrats were supporters.

Final passage came in plenty of time to assure Obama's signature before the administration's 11:59 p.m. Thursday deadline.

That was when Treasury Secretary Jacob Lew said the government would reach the current $16.7 trillion debt limit and could no longer borrow to meet its obligations.

Tea party-aligned lawmakers who triggered the shutdown that began on Oct. 1 said they would vote against the legislation. Significantly, though, Texas Sen. Ted Cruz and others agreed not to use the Senate's cumbersome 18th century rules to slow the bill's progress.

In remarks on the Senate floor, Cruz said the measure was "a terrible deal" and criticized fellow Republicans for lining up behind it.

McConnell made no mention of the polls showing that the shutdown and flirtation with default have sent Republicans' public approval plummeting and have left the party badly split nationally as well as in his home state of Kentucky.

More broadly, national tea party groups and their allies underscored the internal divide. The Club for Growth urged lawmakers to vote against the congressional measure and said it would factor in the organization's decision when it decides which candidates to support in midterm elections next year.

"There are no significant changes to Obamacare, nothing on the other major entitlements that are racked with trillions in unfunded liabilities, and no meaningful spending cuts either. If this bill passes, Congress will kick the can down the road, yet again," the group said.

Democrats quietly recorded a partisan victory. But after a shutdown and debt-limit fight estimated to have sucked as much as $20 billion out of the U.S. economy, there was no celebration.

"It is time to end the hostage-taking, brinksmanship and heated rhetoric and for Congress to demonstrate that it remains capable of running this government, paying the country's bills and caring for its citizens," Sen. Dianne Feinstein, D-Calif., said in a statement after the Senate's vote.

Simplicity at the end, there was next to nothing in the agreement beyond authorization for the Treasury to resume borrowing and funding for the government to reopen.

House and Senate negotiators are to meet this fall to see if progress is possible on a broad deficit-reduction compromise of the kind that has proved elusive in the current era of divided government.

Senate Budget Committee Chairman Patty Murray, D-Wash., was to have breakfast Thursday morning with her House counterpart, Rep. Paul Ryan, R-Wis., to start a new round of talks aimed at averting another crisis. Obama repeated his vow to work with Republicans to rein in a national debt that remains at historically high levels.

"With the shutdown behind us and budget committees forming, we now have an opportunity to focus on a sensible budget that is responsible, that is fair, and that helps hardworking people all across this country," Obama said at the White House.

Bay Area News Group staff and the Washington Post contributed to this report.

Highlights of the deal
Shutdown: Workers return,
agencies financed until Jan. 15
Debt ceiling: Authority to borrow money extended to Feb. 7
Health care law: Department of Health and Human Services must certify it can verify income eligibility of people seeking subsidies
Congress: No pay raise for
members of Congress in 2014
Wildfires: Allocates $636 million for firefighting for the Interior Department and the Forest Service
Kentucky: Increases to $2.9 billion the funding for dams on the Lower Ohio River, which flows through Illinois and Kentucky.
Veterans: Sets aside $294 million for Veterans Affairs to reduce backlogs of benefits claims
Associated Press and Washington Post