[What you need to know to start the day: Get New York Today in your inbox.]

ALBANY — Lawmakers in New York, the epicenter of the nation’s measles outbreak, voted on Thursday to end religious exemptions for immunizations, overcoming opposition by vaccine skeptics and others who said the measure infringed on religious and constitutional rights.

Calling it a public health emergency, Gov. Andrew M. Cuomo immediately signed the bill, adding New York to a small handful of states that do not allow exemptions on religious grounds, including California, Arizona, West Virginia, Mississippi and Maine.

The issue is particularly acute in New York, where many measles cases have originated in Orthodox Jewish communities in Brooklyn and in Rockland County, where so-called vaccine symposiums have featured speakers who encouraged people to shun immunization.

The tension over the issue was readily apparent in the Capitol on Thursday as hundreds of angry opponents — many with young children and infants — pleaded with lawmakers to reject the bill, sometimes invoking the will of God, other times their rights as parents. The show of raw emotion affected even supporters of the bill.

Assemblyman Michael Montesano, a Long Island Republican, framed the bill as “an attack on people’s First Amendment rights.” He added, “It’s still the individual parent, who is raising this child, that has the fundamental right to decide what happens with their child in all facets of their life.”

Others cast their votes in personal terms. Assemblyman Kenneth Zebrowski, a Rockland County Democrat, noted that his county had 266 confirmed cases, with more than a dozen hospitalizations. His 1-year-old daughter had to accelerate her vaccinations, he said, because of the outbreak there.

“Our job is not just to react to epidemics,” Mr. Zebrowski said before voting yes. “Our job as legislators is to prevent epidemics.”

As the Assembly vote slowly came in, the speaker, Carl E. Heastie, was forced to come to the floor and count votes, calling recalcitrant members to coax the bill toward the 76-vote threshold needed for passage. Several prominent Democrats, including the chairman of the health committee, Richard N. Gottfried, bucked Assembly leadership and voted no. In the end, it narrowly passed, 77 to 53.

As soon the vote count was called, shouts of “shame” — and more colorful invective — erupted from the Assembly gallery, where opponents had gathered to watch the proceedings. Assemblyman Jeffrion L. Aubry attempted to restore order, but the screams continued; unable to stop the shouting, Mr. Aubry took the chamber into recess as furious opponents headed into Capitol hallways.

The State Senate, where the vote was assured because of solid support in a Democratic majority, approved the bill, 36-26. Mr. Cuomo, a three-term Democrat, signed the legislation moments after it passed the Senate, saying that vaccines “are safe, effective and the best way to keep our children safe.”

“While I understand and respect freedom of religion, our first job is to protect the public health,” Mr. Cuomo said in a statement, adding that the new law “will help prevent further transmissions and stop this outbreak right in its tracks.”

The current measles outbreak has spread to 28 states, with more than 1,000 cases in total, the highest number since 1992, when more than 2,000 cases were recorded. Highly contagious, measles can lead to serious complications, including pneumonia and encephalitis, according to the Centers for Disease Control and Prevention.

A measles outbreak in 2015 in California led that state to revoke nonmedical exemptions. But lawmakers there are now considering tightening those restrictions and forcing parents to seek medical exemptions from the state Department of Public Health, instead of doctors.

The proposal gained attention this week when the actress Jessica Biel met with California lawmakers to lobby against the measure; she said in an Instagram post on Thursday that she supported vaccinations, but believed that families should have the “right to make educated medical decisions for their children alongside their physicians.”

Once a common disease for millions of children each year, measles was declared eliminated in 2000 after several decades of widespread vaccination. But the current outbreak has alarmed state and national health officials, as well as medical advocates.

On Thursday, for example, the American Medical Association announced it would step up its efforts to “incentivize states to eliminate nonmedical exemptions.” The association also said it would support state bills allowing minors to ask for vaccinations even if their parents refuse, which is the subject of another bill pending in New York.

The immunization bill eliminates an exemption for any parent or guardian who holds “genuine and sincere religious beliefs” against immunization. During the 2017-18 school year, 26,217 students in public, private and parochial schools, child care centers, nursery schools and prekindergarten programs in New York State had religious exemptions, according to the State Department of Health.

On Thursday, the health department reiterated that “immunizations give children the best protection from serious childhood diseases and are safe and effective,” but officials also seemed cognizant of ongoing skepticism in some quarters.

“The New York State Department of Health will continue our extensive public outreach campaign to educate people on the facts about vaccinations and to counter misinformation that has fueled this outbreak,” said Jill Montag, a department spokeswoman.

‘Monkey, Rat and Pig DNA’: How Misinformation Is Driving the Measles Outbreak Among Ultra-Orthodox Jews

Bastion of Anti-Vaccine Fervor: Progressive Waldorf Schools

State Senator Brad Hoylman, the sponsor of the bill in that chamber, cast the bill as an effort to fight debunked online theories about vaccines causing autism and other harmful side effects.

“There is a public health crisis underway and New York is the epicenter,” said Mr. Hoylman, a Democrat from Manhattan. “And numbers continue to grow because well-intentioned parents are being misinformed by anti-vax conspiracy theorists. And it’s part of the state’s responsibility to make sure everyone is safe in schools and day care centers.”

The measure in New York had been stalled for years because of sensitivity about ultra-Orthodox groups, which have often wielded political influence, as well as intense efforts by organizations like the Robert F. Kennedy Jr.-led Children’s Health Defense, an anti-vaccination group.

In another moment of legislative drama, the bill was nearly derailed by the Assembly health committee earlier on Thursday, passing only after a member, Nader J. Sayegh, a Yonkers Democrat, changed his vote to yes to break a tie, allowing the bill to proceed. Mr. Sayegh voted no on the bill once it reached the Assembly floor, but said he believed “the public and the Assembly at large deserves an opportunity to vote on this matter.”

Mr. Heastie, the Assembly speaker, had attended the committee meeting to help usher the bill to the floor, in an indication of how close the vote was expected to be.

As committee members entered the meeting, activists chanted, “Please vote no!” and held handmade placards reading “Protect 1st Amendment: God’s Watching.”

Opponents then crowded into the meeting itself, some carrying newborns and small children. Several opponents cried when the measure passed out of committee.

At the hearing, the bill’s sponsor, Assemblyman Jeffrey Dinowitz, a Bronx Democrat, argued that California’s actions increased vaccination rates there, and had helped fend off future outbreaks of the measles, which can be fatal in rare cases. “Vaccinations have saved countless millions of lives,” he said.

Later, as opponents of the bill filled the Assembly gallery, several lawmakers described wrestling with a complicated decision, pitting parental prerogative and public health.

“This is the hardest decision I’m going to be making in my 17 years here,” said Assemblyman Andrew P. Raia, a Long Island Republican who voted no.

Mr. Dinowitz stressed that his bill was not simply a reaction to the current outbreak, noting that he first introduced it in 2015.

“The purpose of the bill,” he said, “is to protect people.”