A 21-year-old rape victim in El Salvador who was suspected of having an abortion after her baby was found dead in the toilet where she gave birth has been cleared of murder in a case that attracted international attention to the country’s strict laws.

Evelyn Beatriz Hernandez was 32 weeks pregnant in 2016 when she felt intense abdominal pains and delivered the newborn into an outdoor toilet. It was later found lifeless in a septic tank.

Her mother found her unconscious daughter next to the latrine and took her to a hospital, where doctors found she had given birth. Hernandez said she did not know she was pregnant.

“If I’d known I was pregnant I would have awaited [the birth] with pride and joy,” she previously said, according to the BBC.

ABANDONED AS A BABY, SHE MET HER FATHER AND FORGAVE HIM IN AN EMOTIONAL REUNION 'WRITTEN BY GOD'

Both women said they didn’t know there was a fetus in the septic tank, but prosecutors did not believe them and pressed charges.

Hernandez, who was 18 at the time, was charged with homicide and sentenced to 30 years in prison in 2017. Her conviction was overturned in February for lack of evidence after serving 33 months of the sentence. A new trial was ordered.

The retrial was a first for such a case in the Central American nation, where prosecutors aggressively pursue legal cases against women who have miscarriages or other obstetric emergencies, accusing them of murder.

HOPE FOR EL SALVADOR: HOW SHARING GOD'S LOVE CHANGED MY LIFE FOREVER

At the retrial, prosecutors sought a harsher 40-year prison sentence, arguing that Hernandez failed to protect her fetus.

On Monday, Hernandez was acquitted by a judge.

"Thank God, justice was done," Hernández said following the announcement of the verdict, visibly emotional as dozens of women waited at the courthouse. "I also thank you who have been present here."

"Yes we did!" the women chanted.

WOMEN 'TIED LIKE PIGS, FORCED ABORTIONS PART OF CHINA'S 'ONE-CHILD POLICY,' DOC SHOWS

El Salvador is one of three Central American nations with total bans on abortion. Women convicted of having abortions face sentences of 2 to 8 years.

But women who turn up at public hospitals following a miscarriage are sometimes accused of having killed the fetus and charged with aggravated homicide, which carries a sentence of 30 to 40 years. Such punishments often fall on poor, young women and victims of rape.

"This is a resounding victory for the rights of women in El Salvador. It reaffirms that no woman should be wrongly accused of homicide for the simple fact of suffering an obstetric emergency," said Erika Guevara-Rosas, Americas director at Amnesty International.

Guevara-Rosas called on El Salvador to cease "criminalizing women once and for all by immediately revoking the nation's draconian anti-abortion laws."

Every year an estimated 25,000 women are impregnated after rapes in the country of just over 6 million. It is believed that thousands of clandestine abortions are carried out each year in El Salvador.

CLICK HERE TO GET THE FOX NEWS APP

Hernandez's case was seen as a test for women's reproductive rights under new President Nayib Bukele, who has said he believes abortion is acceptable only when the mother's life is at risk but that he opposes criminalizing women who have miscarriages.

"If a poor woman has a miscarriage, she's immediately suspected of having had an abortion," Bukele said in 2018. "We can't assume guilt when what a woman needs is immediate assistance."

The Associated press contributed to this report.