A federal judge on Wednesday ordered the U.S. government to allow an undocumented immigrant teenager in its custody to have an abortion and said she was "astounded" that the Trump administration was trying to block the procedure.

Lawyers for the American Civil Liberties Union, which represents the pregnant teen, cheered the judge's ruling as a major victory for abortion and immigrant rights.

"We never should have had to fight this in the first place," said Brigitte Amiri, a senior ACLU staff attorney who argued the case on Wednesday. "It should never have been something that we needed to go to court over."

Late Wednesday, however, the Justice Department appealed the case, asking the U.S. Court of Appeals for the D.C. Circuit to stay District Judge Tanya Chutkan's ruling.

The government asked the appeals court to rule by 9 p.m. on Thursday, to prevent the 17-year-old, identified in court papers as Jane Doe, from having an "irreversible elective abortion" while the appeal is pending. The government said the teenager, who is in her 15th week of pregnancy, "still has a number of weeks in which she could legally and safely obtain an abortion."

Hours earlier, Chutkan had ordered the government to allow the teen to visit the abortion provider located closest to her shelter in Texas on Thursday and undergo state-mandated counseling before terminating the pregnancy on Friday or Saturday. Texas bars most abortions after 20 weeks.

"Failure to comply with the terms of this Order may result in a finding of contempt," wrote Chutkan, who was appointed by President Barack Obama.

[Read Chutkan’s order]

The case of the Central American teen has drawn national attention. Democrats in Congress expressed opposition to the government's stance, while Texas and seven other states filed a friend-of-the-court brief supporting it.

Marjorie Dannenfelser, president of the antiabortion group Susan B. Anthony List, said Chutkan's ruling "sets a dangerous precedent" for officials from the U.S. Department of Health and Human Services, who she said were trying to "protect the life and dignity of the teenage girl and her unborn child."

Court filings make clear that the government is trying to prevent minors in its custody from having abortions, a departure from U.S. practice under Obama. Scott Lloyd, director of the Office of Refugee Resettlement, the HHS agency that cares for unaccompanied minors caught crossing the border, said in an email in March that federally funded shelters "should not be supporting abortion services pre or post-release; only pregnancy services and life-affirming options counseling."

During the hearing Wednesday, Chutkan asked Justice Department lawyer Scott Stewart whether he thought illegal immigrants had constitutional rights and whether he believes that the 1973 Supreme Court ruling Roe v. Wade , which guarantees a woman's right to an abortion, is still the "law of the land."

Stewart acknowledged the ruling but said the government views this case differently because the teen is an undocumented immigrant in federal custody. He signaled that undocumented minors do not have a constitutional right to an elective abortion in federal custody, unless it is a medical emergency, and also said immigrants here illegally have "minimal" protections in this country.

"I'm not going to give you a concession on that, Your Honor," he said.

The judge laughed. "This is remarkable," she said.

Chutkan said the teen's immigration status was irrelevant and that she still had constitutional rights. She wrote that the teen will "suffer irreparable injury," including health risks, if the government interferes with her abortion plans. Chutkan also barred the government from forcing the teen to reveal her abortion decision to anyone or retaliating against her or the federally funded shelter housing her in Texas. She did not immediately act on an ACLU request to apply her ruling to other minors in federal custody.

By refusing to allow the girl to be transported to have an abortion, Chutkan said, the government appeared to be offering the teenager two options: voluntarily return to a nation she fled to have an abortion; or carry an unwanted pregnancy to term.

"I am astounded by that position," Chutkan said.

In court filings, the Justice Department said the government has "strong and constitutionally legitimate interests in promoting childbirth, in refusing to facilitate abortion, and in not providing incentives for pregnant minors to illegally cross the border to obtain elective abortions while in federal custody."

Chutkan countered during the hearing that the teenager does not need a medical emergency to exercise her right to an abortion. She said the teen had followed state and federal rules: She obtained permission from a state judge in Texas to have an abortion and would cover the expenses herself or with help through her court-appointed guardian.

All the government had to do, Chutkan said, is process the paperwork to let the girl visit the clinic, just as they would if she needed to have her tonsils removed.

The judge pointed out that the federal workers took the teenager, against her wishes, to a Christian pregnancy facility for counseling aimed at persuading her not to abort, and also informed her mother about the pregnancy. Both steps potentially violated the girl's constitutional protections, Chutkan said.

"The government didn't seem to have any problem facilitating that," Chutkan said.

The teen has been in federal custody since early September, when she was caught illegally crossing the U.S.-Mexico border. Her native country has not been made public.