A fifth American was released separately.
The negotiated exchange eases one leading irritant as the two countries gingerly explore prospects for a smoother relationship after decades of hostility even as they remain sharply at odds on other fronts.
Washington Post reporter Jason Rezaian, former US Marine Amir Hekmati, pastor Saeed Abedini and Nosratollah Khosravi- Roodsari, whose name had not been previously made public, were freed from custody in Iran and were to be flown to Switzerland, US officials said.
In turn, the US will pardon or drop charges against seven Iranians six of whom are dual US-Iranian citizens accused or convicted of violating US sanctions.
Also Read
Three were serving prison terms and now have received a commutation or pardon. Three others were awaiting trial; the last one made a plea agreement.
It's unclear if they will leave the US for Iran. They are free to stay in the US.
In addition, the US will drop Interpol "red notices" essentially arrest warrants on 14 Iranian fugitives it has sought, officials said.
US Secretary of State John Kerry, Iranian Foreign Minister Mohammad Javad Zarif and other officials involved in the accord met in Vienna as the diplomatic achievement unfolded.
The release of the prisoners and the nuclear deal developments capped weeks of intense US-Iran diplomacy that took several unexpected turns after an Iranian ballistic missile test in October and then the detention on January. 12 by Iran of 10 US Navy sailors and their two boats in the Persian Gulf.