“When agents of the United States government violate fundamental rights of Mexican nationals and others within Mexico’s jurisdiction, it is a priority to Mexico to see that the United States provides adequate means to hold the agents accountable and to compensate the victims,” the brief said. “The United States would expect no less if the situation were reversed and a Mexican government agent, standing in Mexico and shooting across the border, had killed an American child standing on U.S. soil.”

Justice Alito also wrote that “the issue here implicates an element of national security.”

“Unfortunately, there is also a large volume of illegal cross-border traffic,” he wrote. “During the last fiscal year, approximately 850,000 persons were apprehended attempting to enter the United States illegally from Mexico, and large quantities of drugs were smuggled across the border. In addition, powerful criminal organizations operating on both sides of the border present a serious law enforcement problem for both countries.”

Chief Justice John G. Roberts Jr. and Justices Clarence Thomas, Neil M. Gorsuch and Brett M. Kavanaugh joined the majority opinion.

In a concurring opinion, Justice Thomas, joined by Justice Gorsuch, called on the court to overrule the Bivens decision entirely.

In dissent, Justice Ruth Bader Ginsburg wrote that the parents’ lawsuit was authorized by the Bivens ruling. Indeed, she said, Mr. Mesa’s lawyer had acknowledged that the suit would have been proper had the shooting occurred in the United States.

“The only salient difference here: the fortuity that the bullet happened to strike Hernández on the Mexican side of the embankment,” Justice Ginsburg wrote. “But Hernández’s location at the precise moment the bullet landed should not matter one whit.”

Justices Stephen G. Breyer, Sonia Sotomayor and Elena Kagan joined the dissent.

The justices also ruled, along the same divided lines, that an Arizona death row inmate, James McKinney, was not entitled to be resentenced by a jury after a federal appeals court ruled that the trial judge who had condemned him to death failed to take into account evidence of abuse Mr. McKinney had endured as a child.