Republicans were also furious that the measure was attached to an essential $681 billion military policy bill, and accused Democrats of legislative blackmail.

Even some Republican members of the usually collegial House Armed Services Committee who helped write the broader legislation, which authorizes military pay, weapons programs and other necessities for the armed forces, opposed the bill in the end, solely because of the hate crimes provision.

“We believe this is a poison pill, poisonous enough that we refuse to be blackmailed into voting for a piece of social agenda that has no place in this bill,” said Representative Todd Akin of Missouri, a senior Republican member of the committee.

On the final vote, 237 Democrats were joined by 44 Republicans in support of the bill; 131 Republicans and 15 Democrats opposed it. The Democratic opponents were a mix of conservatives who were against the hate crimes provision and liberals opposed to Pentagon provisions.

The military bill has yet to be approved by the Senate. But the hate crimes provision has solid support there, and Senator John McCain of Arizona, the senior Republican on the Senate Armed Services Committee, said the overall bill outweighed his own objections to including the hate crimes measure.

Mr. Obama supports the hate crimes provision, though the White House has raised objections to elements of the bill related to military acquisitions. If signed into law, the hate crimes legislation would reflect the ability of Democrats to enact difficult measures with their increased majorities in Congress and a Democrat in the White House.

Image Representative John A. Boehner, the House Republican leader, voiced opposition to the measure. Credit... Evan Vucci/Associated Press

“Elections have consequences,” Mr. McCain said.

Similar hate crime provisions have passed the House and the Senate in previous years but have never been able to clear their final hurdles. Speaker Nancy Pelosi said Thursday that it was fitting that Congress was acting now, since next Monday is the 11th anniversary of Matthew Shepard’s killing. The hate crimes part of the bill is named for Mr. Shepard and James Byrd Jr., a black man killed in a race-based attack in Texas the same year.