U.S. President Barack Obama has signed a measure into law reopening the federal government and averting a potential default.

The Senate voted 81-18 to send the measure to the Republican-controlled House of Representatives, which passed it late Wednesday night 285-144. Obama signed the measure — which adhered strictly to the terms he laid down when the twin crises erupted more than three weeks ago — shortly after midnight Thursday.

Congress had faced a deadline of 11:59 p.m. on Thursday to raise the government's borrowing authority or risk a default on its obligations.

We fought the good fight. We just didn't win. - House Speaker John Boehner

The bill reopens the government through Jan. 15 and permits the Treasury to borrow normally through Feb. 7 or perhaps a month longer. It includes nothing for Republicans demanding to eradicate or scale back Obama's signature health care overhaul.

"We fought the good fight. We just didn't win," conceded House Speaker John Boehner as lawmakers lined up to vote on the bill.

16-day shutdown ends

Less than an hour later, as debate began in the House, Republican Congressman Harold Rogers 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."

Senate Majority Leader Harry Reid gestures during a news conference with, from left, Senator Chuck Schumer, Senator Patty Murray, and Senator Dick Durbin after passing a bill to raise the debt ceiling and fund the government on Capitol Hill on Wednesday. (Evan Vucci/Associated Press)

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

The crisis began on Oct. 1 with a partial shutdown of the federal government after House Republicans refused to accept a temporary funding measure unless Obama agreed to defund or delay his health care law, known as "Obamacare." It escalated when House Republicans also refused to move on needed approval for raising the amount of money the Treasury can borrow to pay U.S. bills, raising the spectre of a catastrophic default. Obama vowed repeatedly not to pay a "ransom" in order to get Congress to pass normally routine legislation.

The hard-right tea party faction of House Republicans, urged on by conservative Senator Ted Cruz of Texas, had seen both deadlines as weapons that could be used to gut Obama's Affordable Care Act, designed to provide tens of millions of uninsured Americans with coverage. The Democrats remained united against any Republican threat to Obama's signature program, and Republicans in the House could not muster enough votes to pass their own plan to end the impasse.

More than two million federal workers — those who had remained on the job and those who had been furloughed — would be paid under the agreement.

Republican approval in steep decline

Boehner and the rest of the top Republican leadership told their rank and file they would vote for the measure. But he vowed Republicans were not giving up on the fight to bring down U.S. debt and cripple "Obamacare," as the president's signature health care overhaul is known.

"Our drive to stop the train wreck that is the president's health care law will continue," Boehner said in a statement.

Harry Reid, the Democratic Senate majority leader, thanked Senator Mitch McConnell, the Republican minority leader, for working with him to end what had become one of the nastiest partisan battles in recent Washington history.

"This is a time for reconciliation," Reid said.

A long line of polls charted a steep decline in public approval for Republicans in the course of what Republican Senator John McCain pronounced a "shameful episode" in U.S. history.

The deal ends the bitter standoff for now, giving both parties time to cool off and come up with a broader budget plan or risk repeating the damaging cycle again in the new year.

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."

McConnell said the time had come to back away for now from Republican efforts to undermine "Obamacare." But the feisty minority boss said Republicans had not given up on erasing it from the legislative books.

Passage in the House depended heavily on minority Democrats to support it. The risky move was seen as imperilling the House leadership, but Boehner was ready to end the crisis that had badly damaged Republican approval among voters.

Looking forward, lawmakers were also concerned voters would punish them in next year's congressional elections. Polls show the public more inclined to blame Republicans.

Republican Senator Lindsey Graham said the party had hurt its cause through the long and dangerous standoff.

"This package is just a joke compared to what we could have gotten if we had a more reasonable approach," he said.