After 16 hours of deliberation, Argentina’s Senate narrowly rejected a bill to legalize abortion on Thursday, dealing a painful defeat to a vocal grass-roots movement that pushed reproductive rights to the top of Argentina's legislative agenda and galvanized abortion rights activist groups throughout Latin America, including in Brazil and Chile.

As legislators debated the bill into the early hours of Thursday morning, thousands waited outside the Congress Building in Buenos Aires, weathering the winter cold.

Supporters of the legislation, which would have legalized abortion care during the first 14 weeks of pregnancy, had hoped Argentina would begin a sea change in reproductive rights in a largely Catholic region where 97 percent of women live in countries that ban abortion or allow it only in rare instances.

In the end, 38 legislators voted against legalization, 31 voted in favor, and 2 legislators abstained.

Opposition in Argentina hardened as Catholic Church leaders spoke out forcefully against abortion from the pulpit and senators from conservative provinces came under intense pressure to stand against legalization.

While the bill's failure is considered a major setback for the activists who backed it, analysts said the abortion rights movement has already brought change to Central and South America in ways that would have been impossible just years ago.

On Wednesday, demonstrators rallied in support of the Argentine bill in Uruguay, Mexico, Peru, and Chile, where they gathered in front of the Argentine Embassy in Santiago, chanting and wearing the green handkerchiefs that became the symbol of Argentina’s abortion rights movement.

Recently, activists in Argentina scored a victory with the passage of a law that seeks to have an equal number of male and female lawmakers.

"If we make a list of the things we’ve gained and the things we’ve lost, the list of things we’ve gained is much bigger,” said Edurne Cárdenas, a lawyer at the Center for Legal and Social Studies, a human rights group in Argentina that favors legalized abortion. “Sooner or later, this will be law.”

In the region, only Uruguay, Cuba, Guyana and Mexico City allow any woman to have an early-term abortion.

For Argentina, the debate over abortion has tugged at the country’s sense of self. It is the birthplace of Pope Francis, the leader of the world’s Catholics, who recently denounced abortion as the “white glove” equivalent of the Nazi-era eugenics program. Recently, though, the country has begun shifting away from its conservative Catholic roots. In 2010, Argentina became the first country in Latin America to allow gay couples to wed. Francis, then the archbishop of Buenos Aires, called that bill a “destructive attack on God’s plan.”

The organized movement that pushed the failed bill started in 2015 with the brutal murder of a pregnant 14-year-old girl by her teenage boyfriend. Her mother claimed the boyfriend’s family didn’t want her to have the baby. As debates about violence against women on social media grew into wider conversations about women’s rights, young female lawmakers gave a fresh push to an abortion bill that had been presented repeatedly in the past without going anywhere.

In June, the lower house of the Argentine Congress narrowly approved a bill allowing women to terminate pregnancy in the first 14 weeks. Current law allows abortions only in cases of rape or when a mother’s life is in danger. While the measure failed in the Senate this week, it made some inroads: among the senators who voted for it was Cristina Fernández de Kirchner, who as president had opposed legalizing abortion.

“Society as a whole has moved forward on this issue,” said Claudia Piñeiro, a writer and abortion-rights activist in Argentina. “Church and state are supposed to be separate, but we’re coming to realize that is far from the case,” Ms. Piñeiro said as it became clearer that the push for legalization would lose.