Even with the congressional action, the government will continue to maintain robust surveillance power, an authority highlighted by Senator Rand Paul, Republican of Kentucky, whose opposition to the phone records program forced it to be shut down at 12:01 a.m. Monday. Mr. Paul and other critics of the legislation said the government's reach into individuals' lives remained too intrusive.
The bill cleared the Senate 67 to 32 after a fierce floor fight; at least four of the opponents voted no because they felt the bill did not go far enough.
Mr. Obama was quick to praise passage of the legislation and to scold those who opposed it.
"After a needless delay and inexcusable lapse in important national security authorities, my administration will work expeditiously to ensure our national security professionals again have the full set of vital tools they need to continue protecting the country," Mr. Obama said. "Just as important, enactment of this legislation will strengthen civil liberty safeguards and provide greater public confidence in these programs."
For a moment, Rand Paul gets back to his basics
The Senate's longest-serving member, Patrick J. Leahy, the seven-term Democrat of Vermont, said the legislation, which he co-sponsored, represented "the most significant surveillance reform in decades."
The fight for the changes was led largely by Democrats and a new generation of Republicans in the House and the Senate who were elected a decade after the terrorist attacks. Even as threats have multiplied since then, privacy concerns, stoked by reports of widespread computer security breaches at private companies, have shifted public opinion.
"National security and privacy are not mutually exclusive," said Senator James Lankford, Republican of Oklahoma, a freshman who like several other younger Republicans voted against the senior senator from his state. "They can both be accomplished through responsible intelligence gathering and careful respect for the freedoms of law-abiding Americans."
Tuesday's vote was a rebuke to Senator Mitch McConnell, Republican of Kentucky and the majority leader, who, until the end in a bitter floor speech, maintained the bill was a dangerous diminishment of national security. Lawmakers in both parties beat back amendments — one by one — that he insisted were necessary to blunt some of the bill's controls on government spying.
Mr. McConnell blasted his fellow senators — and by association Speaker John A. Boehner, who heartily endorsed the measure — as taking "one more tool away from those who defend our country every day."
"This is a significant weakening of the tools that were put in place in the wake of 9/11 to protect the country," he said. "I think Congress is misreading the public mood if they think Americans are concerned about the privacy implications."
But even scores of senators who loathed the actions of Mr. Snowden voted for the legislation.
Read MoreGCHQ: We don't have the manpower to spy on you
The legislation's goals are twofold: to rein in aspects of the government's data collection authority and to crack open the workings of the secret national security court that oversees it. After six months, the phone companies, not the N.S.A., will hold the bulk phone records — logs of calls placed from one number to another, and the time and the duration of those contacts, but not the content of what was said. A new kind of court order will permit the government to swiftly analyze them.
The Foreign Intelligence Surveillance Court, for the first time, will be required to declassify some of its most significant decisions, and outside voices will be allowed to argue for privacy rights before the court in certain cases.
The battle over the legislation, the USA Freedom Act, made for unusual alliances. Mr. Boehner joined forces with Mr. Obama, the bipartisan leadership of the House Judiciary Committee, and a bipartisan coalition of senators against Mr. McConnell and his Intelligence Committee chairman, Senator Richard Burr, Republican of North Carolina.
Mr. McConnell made a series of miscalculations, stretching back to last year, when he filibustered a similar surveillance overhaul measure. Last month, after Republicans blocked consideration of the Freedom Act, Mr. McConnell sent the Senate on a weeklong Memorial Day recess, pushing Washington up against a June 1 deadline, when surveillance authority would lapse.
That empowered Mr. Paul, who promised supporters of his presidential campaign that he would single-handedly ensure that surveillance authority lapsed, a promise on which he delivered. When Mr. McConnell then argued in favor of amending the Freedom Act, senators in both parties — even some who supported him — said any changes would only extend the surveillance blackout and risk the country's security.