The US and Iran have conducted a prisoner swap in a rare sign of co-operation between the two countries.
The exchange involved a Chinese-American researcher convicted of spying in Iran and an Iranian scientist held by the US. Both deny wrongdoing.
Iran's foreign minister said he was glad as he announced the exchange.
Hours later, US President Donald Trump tweeted: "Thank you to Iran on a very fair negotiation. See, we can make a deal together!"
Who were the prisoners?
Xiyue Wang was arrested in Iran in 2016 for "collaborating with foreign governments".
Massoud Soleimani, a stem cell expert, was arrested at a Chicago airport last year. He was accused violating trade sanctions by trying to export biological material to Iran.
Iran and the US have had an increasingly strained relationship in recent years and share no diplomatic links.
Both have thanked the Swiss government for its assistance as an intermediary facilitator.
Mr Wang was flown in a Swiss government plane from Tehran to Zurich, and then to Ramstein Air Base in Germany, where he will undergo medical check-ups before heading home.
Mr Soleimani was also flown to Zurich and then on to Iran.
Iranian Foreign Minister Mohammad Javad Zarif tweeted photos of himself with Mr Soleimani after his release.
Going home pic.twitter.com/HZHPDc20aj— Javad Zarif (@JZarif) December 7, 2019
He was the first to announce the news, via a tweet. "Glad that Professor Massoud Soleimani and Mr Xiyue Wang will be joining their families shortly," he wrote.
In a formal statement, US President Donald Trump said Mr Wang had been "held under the pretence of espionage".
"Freeing Americans held captive is of vital importance to my Administration, and we will continue to work hard to bring home all our citizens wrongfully held captive overseas," the statement said.
Hua Qu, Mr Wang's wife, wrote in statement: "Our family is complete once again. Our son Shaofan and I have waited three long years for this day and it's hard to express in words how excited we are to be reunited with Xiyue.
"We are thankful to everyone who helped make this happen."
Princeton University, where Mr Wang was studying as a postgraduate, said in a statement it was "overjoyed" with the news of his release and was looking forward to "welcoming him back to campus".
What were the men accused of?
Mr Wang was arrested in Iran in August 2016 as he was leaving the country.
He had been doing research in Iran for a university dissertation and was accused of seeking to gather "highly confidential articles" for US and British academic institutions. He was sentenced to 10 years in jail for spying.
Mr Soleimani was detained in October 2018 on accusations of attempting to export biological materials to Iran in violation of trade sanctions on the country over its nuclear program.
What is the background?
US-Iran tensions have risen significantly in the last two years.
After President Donald Trump took power, the US pulled out of a 2015 treaty that aimed to limit Tehran's nuclear programme.
Tehran has always insisted its nuclear programme is peaceful, but the US voiced concerns about potential weapon building.
Mr Trump also reinstated sanctions on Iran, which have led to its currency plummeting and inflation soaring.
Iran has recently been hit by major protests over rising petrol prices, which are partly linked to the US sanctions.