DURHAM, N.C. — Defying the governor, lawmakers on Thursday enacted a law that allows state court officials to refuse to perform a marriage if they have a “sincerely held religious objection,” a measure aimed at curtailing same-sex unions.

The Republican-controlled House voted 69-41 on Thursday to override a veto by Gov. Pat McCrory, also a Republican, who refused to sign the marriage bill in May. Mr. McCrory said at the time that although he believed that marriage should be between a man and a woman, he vetoed the bill because “no public official who voluntarily swears to support and defend the Constitution and to discharge all duties of their office should be exempt from upholding that oath.”

The State Senate, which is also controlled by Republicans, overrode Mr. McCrory’s veto June 1.

The law, which takes effect immediately, allows magistrates, along with assistant and deputy registers of deeds, to refuse to perform a marriage without facing punishment or charges of willfully failing to discharge their duties. Court officials who disclose a religious objection must stop performing all marriages for at least six months.