British-Iranian nationals Nazanin Zaghari-Ratcliffe and Anoosheh Ashoori are on a plane leaving Iran after being freed, the government has said.
It said it had also settled a debt owed to Iran over a multi-million pound order of tanks dating back to the 1970s.
Mrs Zaghari-Ratcliffe was arrested in 2016 - accused of plotting to overthrow Iran's government, which she denied.
Mr Ashoori was arrested in 2017 and accused of spying, a claim he denied.
A spokesperson for Iran's judiciary said both dual nationals had been released.
A third dual national, Morad Tahbaz, has been released from prison but will not be returning to the UK, Foreign Secretary Liz Truss said.
"We will continue to work to secure Morad's departure from Iran," she tweeted.
Prime Minister Boris Johnson said he was "delighted they will be reunited with their families and loved ones".
Mrs Zaghari-Ratcliffe had been under house arrest and was given her UK passport back this week.
Her husband Richard Ratcliffe, who lives with their six-year-old daughter Gabriella in Hampstead, London, has not yet commented. He had campaigned for her release, including by going on hunger strike in October last year.
Tulip Siddiq, Labour's MP for Hampstead and Kilburn, tweeted a picture of Mrs Zaghari-Ratcliffe and wrote: "Nazanin is now in the air flying away from 6 years of hell in Iran."
"My heart goes out to Gabriella and Richard, as her long journey back home to them gets closer by the minute," she added.
Earlier, Ms Siddiq told the BBC Mrs Zaghari-Ratcliffe had been at the airport with her passport, but remained "under the control of the Iranian Revolutionary Guard".
"I won't rest until she is on British soil, in West Hampstead, in her house," she added.
Mrs Zaghari-Ratcliffe's sister-in-law Rebecca Ratcliffe told BBC News it was an "emotional day".
"It feels like we're on the home run now but until she leaves that airport we can't believe it," she said, adding that she had also spoken to Mr Ratcliffe.
She said Mrs Zaghari-Ratcliffe had been picked up and taken to the airport with her parents, who were not allowed in a holding room with her because she was "still under Iranian control in the airport".
"She's still not free. But it definitely feels like she's about to be," she said.
Earlier Ms Siddiq said Mrs Zaghari-Ratcliffe had been "dreaming" about the day she could return to the UK.
A £400m debt relating to a cancelled order for 1,500 Chieftain tanks dating back to the 1970s had been linked to the continued detention of Mrs Zaghari-Ratcliffe and other UK-Iranian dual nationals held in the country - although the government has previously said the two issues should not be linked.
Ms Truss told the BBC on Wednesday the debt was "legitimate" and that the government was "looking for ways to pay" it.
Labour leader Sir Keir Starmer said it was "an incredible moment" for Mrs Zaghari-Ratcliffe and her family after an "unimaginable ordeal".
He added that there would be questions to be answered about "what happened along the way", but at present his thoughts were with the family.