Here are the key moments from the last day before the financial default deadline, when the Senate and House came together to pass a deal to reopen the government. (The Washington Post)

After shutting down the U.S. government for 16 days and driving the nation toward the brink of default, a chastened Congress voted late Wednesday to reopen federal agencies, call hundreds of thousands of civil servants back to work and raise the $16.7 trillion debt limit.

An agreement struck by Senate Majority Leader Harry M. Reid (D-Nev.) and Minority Leader Mitch McConnell (R-Ky.) ended a stalemate created last month, when hard-line conservatives pushed GOP leaders to use the threat of shutdown to block a landmark expansion of federally funded health coverage.

That campaign succeeded mainly in undermining popular support for the Republican Party, however. By late Wednesday, dozens of anxious GOP lawmakers were ready to give President Obama almost exactly what he requested months ago: a bill to fund the government and increase the Treasury Department’s borrowing power with no strings attached.

“We’ve been locked in a fight over here, trying to bring government down to size, trying to do our best to stop Obamacare,” House Speaker John A. Boehner (R-Ohio) told a Cincinnati radio station. “We fought the good fight. We just didn’t win.”

The Senate overwhelmingly ratified the deal Wednesday evening, 81 to 18, with more than half of Senate Republicans voting yes.

A few hours later, the House followed suit, approving the measure 285 to 144. Eighty-seven Republicans joined a united Democratic caucus in approving the measure, allowing Congress to meet a critical Treasury Department deadline with one day to spare.

Obama signed the measure into law shortly after midnight,, reopening parks and monuments across the nation, restoring government services and putting furloughed federal employees back on the job, many of them in the Washington region.

“Employees should expect to return to work in the morning,” Sylvia Mathews Burwell, the White House budget director, said in a statement.

The measure also guarantees those workers back pay for time spent at home, aid flood-ravaged Colorado and provide extra cash for fighting wildfires out West. And it grants the District government, which relies on Congress to approve its budget, authority to manage its own affairs through the 2014 fiscal year.

Enforcement of the debt limit is suspended until Feb. 7, setting up another confrontation over the national debt sometime in March, independent analysts estimated. Meanwhile, federal agencies are funded through Jan. 15, when they might shut down again unless lawmakers resolve a continuing dispute over deep automatic spending cuts known as the sequester.

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.

Few held out hope that the talks would yield an ambitious plan to overhaul the tax code or restructure federal health and retirement programs, the biggest drivers of future borrowing. But there were signs that Republicans may be more inclined to compromise and less inclined to follow what Rep. Aaron Schock (R-Ill.) called “the fringe elements” of the GOP.

“The reality is there’s a much larger population within our caucus that recognizes reality for what it is,” said Schock, who represents the iconic middle-America town of Peoria. “At the end of the day, whatever we pass will have to be a bipartisan bill. The sooner that our conference recognizes that we’re going to have to negotiate with the other side, the more we can get done.”

The fight over the health-care law that many call Obamacare originated on the Senate side of the Capitol, with Sens. Ted Cruz (R-Tex.) and Mike Lee (R-Utah), as well as former senator Jim DeMint (R-S.C.), who now heads the conservative Heritage Foundation. Cruz and Lee voted against Wednesday’s agreement, as did GOP presidential hopefuls Sens. Marco Rubio (Fla.) and Rand Paul (Ky.).

However, Boehner and the Republican-controlled House waged the fiercer battle, passing bill after bill to defund, delay and otherwise undercut the Affordable Care Act, only to watch those bills die in the Democratic-controlled Senate.

Cruz rejected the idea that Republicans had won nothing during the six-week fight. While the measure contained just one small adjustment to the health-care law — strengthening safeguards against fraud among recipients of federal health insurance subsidies — Cruz said the debate succeeded in calling attention to the harm the law is causing consumers, employers and the U.S. economy.

“We saw the House of Representatives take a courageous stand, listening to the American people,” Cruz said. “Had Senate Republicans united and supported House Republicans, the outcome of this, I believe, would have been very, very different.”

Still, the price the House paid for waging what many Republicans saw as an unwinnable fight was ultimately devastating. Boehner had counseled against shutting down the government as recently as late August, fearing a backlash among voters that quickly materialized in public opinion polls.

Yanked to the right, Boehner tried repeatedly to satisfy Cruz and his allies. He made a last-ditch effort Tuesday to rally his troops around a measure that would have eked out two minor health-care victories: repeal of a tax on medical devices that helps finance the Affordable Care Act and elimination of employer-provided health insurance subsidies for lawmakers, their aides and administration officials. The subsidies, long available to all federal workers, have been derided by conservative commentators as a “special exemption” now that lawmakers are required to get insurance through the new health-care exchanges.

It wasn’t enough. Conservative lawmakers demanded more concessions. More moderate Republicans complained that the plan would never pass the Senate.

On Tuesday night, with House Republicans bitterly divided and Boehner himself badly weakened, the effort collapsed. By Wednesday, Boehner and McConnell had no choice but to seal a deal with Reid or run the risk of inviting economic calamity.

In the House, the vote was eerily similar to the last fiscal showdown with Democrats. During the brief debate, not a single member of the GOP leadership team spoke in favor the bill. That left House Appropriations Chairman Harold Rogers (R-Ky.) alone among senior Republicans to publicly champion the bill, just as Ways and Means Chairman Dave Camp (R-Mich.) had been during the “fiscal cliff” vote on New Year’s Day.

It ended with a bizarre moment, when a House stenographer ran to a microphone and began shouting religious messages. She was forcibly removed from the chamber.

In the Senate, McConnell, a wily tactician unaccustomed to surrender, tried to salvage some sense of accomplishment. As he and Reid announced the deal at midday Wednesday on the Senate floor, McConnell noted that the measure would maintain current spending levels, “protecting the government spending reductions that both parties agreed to under the Budget Control Act” of 2011.

“That’s been a top priority for me and my colleagues on the Republican side of the aisle,” McConnell said.

But Democrats quickly said they would seek to roll back those cuts before the next round of sequester reductions goes into effect Jan. 15. Meanwhile, many Republicans lamented that the misguided attack on the health-care law had cost the party a shot at forcing Democrats to consider a serious debt-reduction plan.

“This package is a joke compared to what we could have gotten if we had a more reasonable approach,” said Sen. Lindsey O. Graham (R-S.C.) “For the party, this is a moment of self-evaluation. We are going to assess how we got here. If we continue down this path, we are really going to hurt the Republican Party long term.”

Democrats, for their part, 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.

“I’m tired,” Reid said after the Senate voted Wednesday night. “Concluding this crisis is historic. But let’s be honest: This was pain inflicted on the nation for no good reason.

“We cannot — we cannot, cannot make the same mistakes again.”

Jackie Kucinich and David A. Fahrenthold contributed to this report.