WASHINGTON – Government officials can detain “sexually dangerous” offenders even after their federal prison terms have been completed, the Supreme Court ruled Monday.

The court had previously decided that states can seek civil commitment of sex offenders they considered still dangerous when their prison terms had been fulfilled. Monday’s 7-2 decision upholding a 2006 federal law said the Constitution grants the same power to the federal government.

“The federal government is the custodian of its prisoners,” Justice Stephen Breyer wrote for the majority. “As federal custodian, it has the constitutional power to act in order to protect nearby (and other) communities from the danger federal prisoners may pose.”

This is true even though the Constitution does not explicitly say so, the court decided.

“Neither Congress’ power to criminalize conduct, nor its power to imprison individuals who engage in that conduct, nor its power to enact laws governing prisons and prisoners, is explicitly mentioned in the Constitution,” Breyer wrote. “But Congress nonetheless possesses broad authority to do each of those things.”

A lower court had held that the federal law, part of the Adam Walsh Child Protection and Safety Act passed in 2006, was unconstitutional, saying the government had exceeded its authority.

Solicitor General Elena Kagan argued for the government, saying its constitutionality was guaranteed by a provision that says Congress may “make all laws which shall be necessary and proper for carrying into execution” its other powers.

Breyer relied several times on her assertions that the law was narrowly tailored to ensure that the federal government was simply being responsible for its prisoners, rather than encroaching on what is normally the duties and powers of states.

For instance, she said, the federal government steps in only when the attorney general has exhausted efforts to persuade states to take responsibility for the sexually dangerous prisoners about to be released.

Kagan was not in the courtroom. She has turned over the daily operations of the solicitor general’s office to her deputy, Neal Katyal, while her nomination to replace retiring Justice John Paul Stevens is pending.

Despite the lopsided margin, there were signs of trepidation. Only Chief Justice John Roberts, Stevens and Justices Ruth Bader Ginsburg and Sonia Sotomayor joined Breyer’s opinion in full.

Justices Anthony Kennedy and Samuel Alito each wrote separately to agree with the outcome but to express reservations about the “necessary and proper clause.”

Justices Clarence Thomas and Antonin Scalia dissented.

In another decision, a complex child custody case split the court in unusual ways, with the majority ruling that, under an international treaty meant to discourage child abduction by battling parents, a Texas mother was wrong to bring her child from Chile to the United States.

The Hague Convention demands that the boy be returned to Chile, the court ruled 6 to 3. But Kennedy, writing for the majority, said the boy’s mother, Jacquelyn Vaye Abbott, could argue to a lower court for an exception that it is not in the boy’s best interest to have to leave the United States.