WASHINGTON—House lawmakers on Wednesday defeated an attempt to drastically curb a national-security program that collects the phone records of millions of Americans, after a tense debate on the balance between privacy rights and government efforts to find terrorists.

The measure was narrowly defeated, 205-217, after last-minute lobbying by the Obama administration and House members on the intelligence panel, who said the program was crucial to national security.

...