Washington (CNN) -- The U.S. Supreme Court ruled Monday the federal government has the power to keep some sex offenders behind bars indefinitely after they have served their sentences if officials determine those inmates may prove "sexually dangerous" in the future.

"The federal government, as custodian of its prisoners, has the constitutional power to act in order to protect nearby (and other) communities from the danger such prisoners may pose," Justice Stephen Breyer wrote for the 7-2 majority.

At issue was the constitutionality of federal "civil commitment" for sex offenders who are nearing the end of their confinement or who are considered too mentally incompetent to stand trial.

The main plaintiff in the case, Graydon Comstock, was certified as dangerous six days before his 37-month federal prison term for processing child pornography was to end. Comstock and the others filing suit remain confined at Butner Federal Correctional Complex near Raleigh, North Carolina.

Three other inmates who filed suit served prison terms of three to eight years for offenses ranging from child pornography to sexual abuse of a minor. Another was charged with child sex abuse but was declared mentally incompetent to face trial.

All were set to be released nearly three years ago, but government appeals have blocked their freedom. The government says about 83 people are being held under the civil commitment program.

Corrections officials and prosecutors determined the men remained a risk for further sexually deviant behavior if freed. The inmates' attorneys maintain the continued imprisonment violates their constitutional right of due process and argue Congress overstepped its power by allowing inmates to be held for certain crimes that normally would fall under the jurisdiction of state courts.

The law in question is the 2006 Adam Walsh Child Protection and Safety Act, which includes a provision allowing indefinite confinement of sex offenders. A federal appeals court in Richmond, Virginia, ruled lawmakers had overstepped their authority by passing it, prompting the current high court appeal.

"The statute is a 'necessary and proper' means of exercising the federal authority that permits Congress to create federal criminal laws, to punish their violation, to imprison violators, to provide appropriately for those imprisoned and to maintain the security of those who are not imprisoned but who may be affected by the federal imprisonment of others," Breyer wrote.

Breyer equated the federal civil commitment law to Congress' long-standing authority to provide mental health care to prisoners in its custody, if they might prove dangerous, "whether sexually or otherwise."

In dissent, Justice Clarence Thomas said the federal government overstepped its bounds.

"Congress' power, however, is fixed by the Constitution," Thomas wrote. "It does not expand merely to suit the states' policy preferences, or to allow state officials to avoid difficult choices regarding the allocation of state funds."He was joined by Justice Antonin Scalia.

The case represented a victory for the federal government and the woman who argued the case on its behalf, Solicitor General Elena Kagan. President Obama nominated Kagan last week to serve on the Supreme Court.

A key lawmaker behind the Adam Walsh act applauded the ruling.

"The court's holding today is a victory on behalf of the American people," said Sen. Patrick Leahy, D-Vermont, chairman of the Senate Judiciary Committee.

"The process to enact this law to protect our children from those who would do them harm was difficult. I am heartened to see an overwhelming majority of the Supreme Court uphold this important child protection law."

The justices in April 2009 had blocked the imminent release of dozens of sex offenders who had served their federal sentences after the Obama administration claimed many of them remain "sexually dangerous." Chief Justice John Roberts ordered the men be kept in custody while the case worked its way to the high court.

Most violent sex offenses are handled at the state level, and at least 20 states run programs in which sexual predators are held indefinitely or until they are no longer considered dangerous. The federal government's civil commitment program is relatively new.

The Adam Walsh act was named after the son of John Walsh, host of TV's "America's Most Wanted."Adam Walsh was kidnapped and murdered by a suspected child molester in 1981.

The act also increased punishments for certain federal crimes against children and created a national registry for sex offenders. Those aspects of the bill were not being challenged in this case.