In 2006, the Bush administration sent five of the Uighurs to Albania, but destinations for the rest remained elusive. In 2008, a federal district court judge ordered the remaining 17 be brought into the United States. But in February 2009, the federal appeals court for the District of Columbia overturned that ruling, saying the judiciary could not order the executive branch to admit a foreigner into the country. The Supreme Court declined to review the matter.

Meanwhile, in the spring of 2009, the Obama administration nearly resettled two of the men in the care of a Uighur community in Northern Virginia, as a test case and in hopes of inducing other countries to take detainees. But a congressional backlash scuttled the plan and helped foster an atmosphere in which lawmakers imposed transfer restrictions on detainees.

Greg Craig, who was Mr. Obama’s White House counsel in the first year of the administration and was closely involved in efforts to resolve the Uighurs’ fate, celebrated the departure of the last Uighurs from Guantánamo.

“From the beginning, we knew that one test of our determination to close Guantánamo would be measured by what happened to the Uighurs,” Mr. Craig said of the final transfers. “That the last of the Uighurs has now left Guantánamo is an important milestone. They didn’t belong there in the first place.”

Amid intense lobbying by the United States, in 2009 four were sent to Bermuda and six to Palau, and in 2010 two went to Switzerland. In a twist, however, releasing the rest became a problem because the remaining five detainees refused offers to go to certain countries, extending their imprisonment for years.

All five rejected offers to go to Palau or the Maldives, officials said. Last year, El Salvador offered to take them in, and two accepted that offer while the final three are said to have rejected that opportunity, too, and held out for something they liked better.

Last summer, according to an American official familiar with the matter, Costa Rica offered to take the remaining three, and the deal advanced enough that in September the Obama administration notified Congress that it intended to transfer them. But China pressured Costa Rica to withdraw its offer, and it did so.