Later on Saturday in the White House Rose Garden, Mr. Obama, flanked by Robert and Jani Bergdahl, the sergeant’s parents, said, “Right now, our top priority is making sure that Bowe gets the care and support that he needs, and that he can be reunited with his family as soon as possible.”

The Bergdahls, who have waged a tireless campaign for their son’s release, have sometimes criticized the Obama administration for lack of action. But at the impromptu Rose Garden appearance and in a statement released earlier in the day, they praised the American and Qatari governments for their help. “We cannot wait to wrap our arms around our only son,” they said in the statement. “Today, we are ecstatic!”

Family and friends in the Bergdahl family’s hometown, Hailey, Idaho, said they were planning a celebration on Sunday. A senior Defense Department official said on Sunday morning that Sergeant Bergdahl had arrived at Landstuhl Regional Medical Center in Germany. A military official said he would then be transferred to Brooke Army Medical Center in San Antonio, Texas, when doctors felt he was fit to travel. Sergeant Bergdahl is expected to reunite with his family in the United States, the official said.

Negotiations and internal deliberations over the potential for a swap have waxed and waned for years, but they intensified in the past several weeks as an agreement appeared within reach, according to an official familiar with the matter.

Among other complications, there was a potential legal obstacle: Congress has imposed statutory restrictions on the transfer of detainees from Guantánamo Bay. The statutes say the secretary of defense must determine that a transfer is in the interest of national security, that steps have been taken to substantially mitigate a future threat by a released detainee, and that the secretary notify Congress 30 days before any transfer of his determination.

In this case, the secretary, Chuck Hagel, acknowledged in a statement that he did not notify Congress ahead of time. When Mr. Obama signed a bill containing the latest version of the transfer restrictions into law, he issued a signing statement claiming that he could lawfully override them under his executive powers.