Catholic Argentina's lower house has backed a bill legalising abortion in the first 14 weeks of pregnancy.
After a divisive debate lasting more than 22 hours, 129 members of the Chamber of Deputies voted in favour and 125 against while one abstained.
The bill will now have to go to the Senate.
President Mauricio Macri is strongly opposed to the bill but has said that he would not veto it if it was passed by both houses.
Abortion is currently illegal in Argentina, except in cases of rape or when the life or health of the woman is at risk. Women seeking abortions also have to apply to a judge for permission, which critics say can unnecessarily delay the procedure.
Up until three hours before the vote, those against the bill seemed to be in the majority until a lawmaker in the province of La Pampa, Sergio Ziliotto, announced on Twitter that he and two colleagues had changed their minds and would vote "yes".
Junto a Melina Delú y Ariel Rauschenberger, los 3 diputados nacionales peronistas por La Pampa votaremos a FAVOR de la despenalización del aborto.— Sergio Ziliotto (@ZiliottoSergio) June 14, 2018
The announcement invigorated pro-choice lawmakers at a time when despondency was beginning to set in after the night-long debate.
Supporters of the bill, many of whom had spent the entire night demonstrating outside of the Congress building in Buenos Aires, cheered and hugged when the result of the vote was announced.
Many also took to social media to celebrate with #AbortoSeraLey (#AbortionWillBeLaw), a hashtag that is trending on Twitter in Argentina.
Among those celebrating the vote as a "historic step" was the secretary general of human rights group Amnesty International, Salil Shetty.
Historic step forward for women's rights in #Argentina today! It's not over yet, but the vote to decriminalise abortion up to 14 weeks sends a powerful message which will reverberate across the whole region #AbortoSeraLey https://t.co/cwBM19SWGn pic.twitter.com/VFKoUubykF— Salil Shetty (@SalilShetty) June 14, 2018
There was high drama even as the result came in. The electronic board inside the chamber initially showed 131 in favour and 123 against but two lawmakers immediately shouted that their vote had not been recorded accurately.
The speaker then proceeded to ask a number of deputies to confirm how they had voted and after some tense minutes confirmed that the bill had been passed.
The bill will now go to the Senate, where it is expected to be put to a vote in September.
Analysts say it faces an uphill battle in the upper chamber, where a number of senators have already expressed their opposition.
Women's rights activists said they would continue to campaign vigorously for the bill to be passed.
They argue that legalising abortions is a public health issue, with many women currently taking huge risks with their health and sometimes even their lives when they resort to illegal terminations.
Some of the lawmakers taking part in the debate said they had been swayed by the argument. They said that while they personally remained opposed to abortions, they saw the need for women to have access to safe, legal terminations.
But many others did not change their minds. Horacio Goicoechea of the Radical Civic Union made an impassioned plea for his colleagues to vote "no", saying: "We're building a law on innocent blood."