WASHINGTON — When President Obama flew to Boise, Idaho, for a speech last winter, he met privately with the wife of an Iranian-American pastor held prisoner in Iran since 2012. Freeing her husband, he promised, was one of his top priorities.
A year later, Mr. Obama called the wife, Naghmeh Abedini. Her husband, Saeed Abedini, was free and would soon be coming home, the president told her. It was a short but emotional phone call. "I could see his love and compassion as he spoke last year and again today," Mrs. Abedini wrote on her Facebook page on Sunday.
But the president's compassion came with a cost. To secure the release of Mr. Abedini and other Americans held by Iran, Mr. Obama freed seven Iranian and Iranian-American men charged with or convicted of violating sanctions against the Islamic republic. Mr. Obama again decided to trade for Americans in captivity despite concerns, even inside his own administration, that it might encourage others to target Americans.
Several leading Republican presidential candidates, including Donald J. Trump and Senators Ted Cruz and Marco Rubio, criticized the trade.
"Governments are taking Americans hostage because they believe they can gain concessions from this government under Barack Obama," Mr. Rubio said in Iowa. "It's created an incentive for more people to do this in the future."
But the families, supporters and administration officials said sometimes a trade is worthwhile. Aides said Mr. Obama was haunted by the possibility that Mr. Abedini and other Americans would spend years in Iran's notorious Evin Prison. While he refused to release violent criminals or terrorists, Mr. Obama deemed the release of a few sanctions-busters a reasonable trade-off, despite the complaints of critics.
"You hear that a lot, and I understand the point," said Representative Dan Kildee, a Michigan Democrat who flew to Germany with the family of Amir Hekmati, another freed American. "This is a complicated world, and we can attempt to deny the way the world works, or we can deal with the reality that there are people out there that we don't like that we have to deal with."
Few dilemmas are more difficult for a president than deciding whether to barter for the freedom of Americans held abroad at the risk of submitting to international blackmail. During the Cold War, it usually involved soldiers or spies, sometimes at Glienicke Bridge in Berlin, as in the Tom Hanks movie "Bridge of Spies," which recounts the trade for Francis Gary Powers, the U-2 pilot shot down over the Soviet Union in 1960.
But in recent decades, presidents have confronted civilians held by terrorists or hostile governments as geopolitical pawns. President Jimmy Carter agonized over embassy workers held hostage by Iran for 444 days and sent an ill-fated rescue mission. President Ronald Reagan was so consumed with American hostages held in the Middle East that he traded arms with Iran to secure their release.
Reagan also freed a Soviet spy in 1986 in exchange for Nicholas Daniloff, a correspondent for U.S. News & World Report arrested in Moscow. Much like Mr. Obama, Reagan was criticized for trading a guilty person for an innocent one, even by conservatives like Representative Jack Kemp.
"If Reagan had gone with Kemp, I would have been sitting in Siberia for a long time," Mr. Daniloff, now 81 and retired in Cambridge, Mass., said by telephone Monday. "I've got to tell you about being arrested and imprisoned — every minute is like a day, every hour is like a year."
But he agreed that a president should make clear such exchanges are not precedent to avoid inspiring more. "Swaps are tricky," he said. "In this particular case, I'm glad that it took place."
Other countries have traded as well. Israel has routinely exchanged imprisoned Palestinians for its own people. In 2011, Israel freed 1,000 Palestinian prisoners, including some it deemed terrorists, for Sgt. Gilad Shalit, who had been held by the militant group Hamas for five years.
Mr. Obama made clear over the weekend that meeting relatives of prisoners has an impact. "I've seen their anguish, how they ache for their sons and husbands," he said. "I gave these families my word — I made a vow — that we would do everything in our power to win the release of their loved ones."
The Iran trade was the latest of several by Mr. Obama. In 2010, on the tarmac of a Vienna airport, his administration swapped 10 Russian sleeper agents arrested in the United States for four Russians held by Moscow for their connections to the West. In May 2014, it traded five Taliban detainees held at Guantánamo Bay, Cuba, for Sgt. Bowe Bergdahl, an American prisoner of war now charged with desertion.
In December 2014, it traded three Cuban spies for a Cuban who had worked as an American agent. Cuba also released Alan Gross, an American contractor accused of spying, but the administration insisted that was not part of the deal because it did not want to equate him with spies.
Veterans of President George W. Bush's administration said such swaps were inherently problematic. "These deals incentivize future hostage taking," said Eric S. Edelman, a former under secretary of defense, and suggest "that Iran can continue to engage in such behavior with impunity."
Stephen Yates, who was a national security aide to Vice President Dick Cheney, said his former boss would have fought such an exchange because swaps "tend to boost the strategic position of our adversary and secure little or no progress in advancing American interests."
In the Iran case, the two countries were reaching closure on many disputes that had divided them for decades. In addition to releasing the seven, the administration withdrew notices seeking the arrest of 14 fugitives overseas. Attorney General Loretta E. Lynch rejected the release of several others.
The deal came on the same weekend that Iran fulfilled the terms of last year's nuclear agreement and the United States lifted sanctions, giving it access to as much as $100 billion in frozen overseas funds. The United States also agreed to pay $1.7 billion to settle a 1981 claim over payments for military equipment that was never delivered.
But the decks were not completely cleared. Robert A. Levinson, a C.I.A. contractor who disappeared in Iran in 2007, was not part of the swap; Iran insists it does not know his whereabouts. And Siamak Namazi, an Iranian-American consultant arrested in Tehran in October, was not part of the exchange.
Former Representative Peter Hoekstra, a Michigan Republican who led the House Intelligence Committee, said that a swap involved difficult trade-offs but that if Mr. Obama made one, he should have insisted on getting all imprisoned Americans out.
"I'm O.K. with negotiations and getting them back," Mr. Hoekstra said. "But how can you at this point go through all the negotiations and leave two behind? What's the calculus there? It should be let's get them all back or no deal."
Secretary of State John Kerry said some Iranians released by the United States were close to completing their sentences, while the fugitives were beyond American reach. "What we gave up, we believe, were people who were about to get out anyway and people that we couldn't get our hands on," he told Fox News.
He denied that the exchange would encourage more Americans to be taken. But he acknowledged that there would probably be such situations again. "That, unfortunately," he said, "is part of the reality of today's life on a global basis."