Tehran on Saturday announced the release of Iranian scientist Massoud Soleimani from the US, shortly before Washington said Princeton graduate student Xiyue Wang was returning home.
"Glad that Professor Massoud Soleimani and Mr. Xiyue Wang will be joining their families shortly," Iranian Foreign Minister Mohammad Javad Zarif wrote on Twitter.
"Many thanks to all engaged, particularly the Swiss government," wrote Zarif, who also posted pictures of himself with Soleimani talking on board an Iranian plane.
Separately, US President Donald Trump said in a statement issued by the White House on Saturday that Wang was "returning to the United States".
"Mr. Wang had been held under the pretense of espionage since August 2016," Trump said. "We thank our Swiss partners for their assistance in negotiating Mr. Wang's release with Iran."
Later on Saturday, Trump thanked Iran "on a very fair negotiation".
"See, we can make a deal together!" he wrote on Twitter.
Tensions have been high between the US and Iran since Trump unilaterally withdrew his country from Tehran's nuclear deal with world powers in May 2018.
Since then, the US has imposed harsh sanctions on Iran as part of a "maximum pressure" campaign to force Iran back to the negotiating table.
Al Jazeera's Assed Baig, reporting from Tehran, said Saturday's move "could be a way of Iran showing the will that they are willing to negotiate and talk but they have to be treated with respect and met halfway - and that's exactly what's happened here".
Wang was sentenced to 10 years in prison in Iran for allegedly "infiltrating" the country and sending confidential material abroad. His family and Princeton University strongly denied the claims.
Soleimani - who works in stem cell research, haematology and regenerative medicine - was arrested by US authorities on charges he had violated trade sanctions by trying to have biological material brought to Iran.
The scientist and his lawyers maintained his innocence, saying he seized on a former student's plans to travel from the US to Iran in September 2016 as a chance to get recombinant proteins used in his research for a fraction of the price he would pay at home.
In a statement, Hua Qu, Wang's wife, said the "family is complete once again."
"We are thankful to everyone who helped make this happen," she said.
Trump is to welcome Wang in person when he arrives in the US after a stop in Germany for medical evaluations.