Evelyn Beatriz Hernandez, now 21, had served 33 months of a 30-year prison sentence when her conviction was overturned in February for lack of evidence and a new trial was ordered.

A young rape victim who was suspected of having an abortion and charged with homicide has been acquitted by a judge at a retrial.

ASSOCIATED PRESS Evelyn Beatriz Hernandez has been acquitted over her baby's death at retrial

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.

“Thank God, justice was done,” Hernandez 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.

Hernandez also thanked foreign diplomats who have followed the case closely.

Victims of alleged sexual assault usually remain anonymous, but Hernandez has spoken publicly about her case.

Hernandez’s fetus was at 32 weeks in 2016 when she felt intense abdominal pains and gave birth in an outdoor toilet. The baby was later found lifeless in a septic tank.

Hernandez said she did not know she was pregnant and her mother said she found her passed out next to the toilet.

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

Forensic experts were unable to determine whether it died in the uterus or in the septic tank.

At the retrial, prosecutors’ argument against Hernandez was commission by omission - that is, she failed to protect her fetus.

“We believe the judge has been very fair in his ruling,” defence lawyer Bertha Maria Deleon said. “He has said that there was no way to prove a crime and for that reason he absolved her.”

El Salvador is one of three Central American nations with total bans on abortion. Women convicted of having abortions face sentences of two to eight 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 “criminalising women once and for all by immediately revoking the nation’s draconian anti-abortion laws”.

El Salvador is a deeply religious country with 80% of the population identifying as Catholic or evangelical Christian. It is also home to macho attitudes on women’s role in society, as well as widespread gang violence.

“We judge cases based on religious convictions, often for things that should never enter in a courtroom,” defence lawyer Arnau Baulenas said.

“We have to stop using justice to respond to social needs. Justice should do its work with criminals, not innocent people.”

Every year an estimated 25,000 women are impregnated after rapes in the country of just over six million.

It is believed that thousands of clandestine abortions are carried out each year in El Salvador.

Recent polls have shown broad support for more lenient abortion laws, though many in the country believe rape victims should be made to carry pregnancies to term.