VIENNA (AP) — Four Americans detained in Iran will be coming home and seven Iranians in U.S. custody also will win their freedom in a breakthrough swap negotiated by the longtime foes, officials in both countries said. As well, a fifth American was freed separately.

The news emerged as a landmark deal took effect Saturday relieving sanctions on Iran in return for its progress in pulling back its nuclear program.

Washington Post reporter Jason Rezaian, former U.S. 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, U.S. officials said. U.S. student Matthew Trevithick was released independently of the exchange on Saturday and already was on his way home.

In turn, the U.S. will pardon or drop charges against seven Iranians — six of whom are dual U.S.-Iranian citizens — accused or convicted of violating U.S. sanctions.

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 U.S. for Iran. They are free to stay in the United States.

In addition, the U.S. will drop Interpol “red notices” — essentially arrest warrants — on 14 Iranian fugitives it has sought, officials said.

The announcement of the exchange came shortly before Iran was certified as having met all commitments under the nuclear deal with six world powers.

U.S. 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 U.S.-Iran diplomacy that took several unexpected turns after an Iranian ballistic missile test in October and then the detention on Jan. 12 by Iran of 10 U.S. Navy sailors and their two boats in the Persian Gulf.

Frederick J. Ryan, Jr., publisher of The Washington Post, said in a statement, “We couldn’t be happier to hear the news that Jason Rezaian has been released from Evin Prison. Once we receive more details and can confirm Jason has safely left Iran, we will have more to share.”

Hekmati’s family released a statement saying: “We thank everyone for your thoughts during this time. There are still many unknowns. At this point, we are hoping and praying for Amir’s long-awaited return.”

Trevithick, the student from Hingham, Massachusetts, went to Iran in September for a four-month language program at an institute associated with Tehran University, his family said in a statement. It said he was held for 40 days in Evin Prison, but gave no reason for his detention.

Negotiations over detainees grew out of the Iran nuclear talks. In discussions in Europe and elsewhere, Kerry and nuclear negotiator Wendy Sherman were able to establish a separate channel of talks that would focus on the U.S. citizens.

American officials didn’t want the citizens used as leverage in the nuclear talks, and didn’t want to lose their possible release if the talks failed to produce an agreement.

The discussions then gained speed after last July’s nuclear deal. In talks in Geneva and elsewhere, a team led by Obama’s anti-Islamic State group envoy, Brett McGurk, worked on the details of a possible prisoner swap. The Iranians originally sought 19 people as part of the exchange; U.S. officials whittled down the number to seven.

Among American politicians, Republican presidential candidates Donald Trump and Ted Cruz and House Speaker Paul Ryan gave cautious praise to the release of the prisoners, particularly Abedini, but said they never should have been held in the first place. Democratic candidate Bernie Sanders praised diplomacy as the key to solving the detainee issue. Hillary Clinton also welcomed the developments while saying Iran should not be thanked because it should never have detained the Americans.

Robert Levinson, who disappeared in Iran in 2007 while working for the CIA on an unapproved intelligence mission, wasn’t part of the deal. American officials are unsure if the former FBI agent is even still alive. The Iranians have always denied knowing his location.

Levinson’s case was aggressively pursued, officials said, adding that Iran has committed to continue cooperating in trying to determine Levinson’s whereabouts.

The exchange also didn’t cover Siamak Namazi, an Iranian-American businessman who advocated better ties between Iran and the U.S. He was thought to have been arrested in October.

According to the official IRNA news agency, the seven freed Iranians are Nader Modanloo, Bahram Mechanic, Khosrow Afghahi, Arash Ghahraman, Tooraj Faridi, Nima Golestaneh and Ali Saboonchi. It didn’t provide any further details.

The lawyer for Mechanic, who has been jailed since his indictment last April on charges of illegally exporting microelectronics technology to Iran, said his client was “elated” to be pardoned.

“He’s been incarcerated for nine months for a crime that he’s just accused of but did not commit,” said lawyer Joel Androphy. “To me, it’s just an injustice. You would expect this in some third-world country, not the United States.” The Justice Department uses the spelling of ‘Mechanic’ in court filings.

___

Dareini reported from Tehran, Iran; Lee reported from Washington. Donna Cassata and Eric Tucker in Washington, Amy Anthony in Providence, Rhode Island, Adam Schreck in Dubai and George Jahn in Vienna contributed to this report.

___

This story has been corrected with the proper spellings of Mechanic, Khosravi-Roodsari and Trevithick.