HOUSTON — She was already nine weeks into her first trimester by the time she learned she was pregnant. And by then, she was already in federal custody at the border in Texas, one of the multitude of unaccompanied minors caught trying to enter the United States without their parents or relatives. She was 17 years old.

That simple pregnancy test set off a dramatic legal battle between civil rights lawyers and the Trump administration, after the teenager made it known that she wanted an abortion. Lawyers and advocates for the girl accused federal officials of preventing her from having an abortion and of taking extraordinary steps to persuade her and other undocumented pregnant minors to have their babies.

On Tuesday, a federal appeals court in Washington sided with the girl, sending the case back to a lower court, which immediately ordered the Trump administration to allow the girl to obtain an abortion “promptly and without delay.” The ruling may be only one of many legal chapters to come if the Justice Department decides to appeal to the Supreme Court.

The case has become a rare intersection of two of the most politically divisive issues in America — illegal immigration and abortion — and has shed light on the role that the federal government and its contractors have played on the border in facilitating and at times trying to block the abortions of undocumented, unaccompanied teenagers.