Desperate, she called home from the Islamic State stronghold of Mosul in northern Iraq begging for help, and was ultimately rescued by Kurdish forces.
On Friday, foreign ministry spokeswoman Veronica Nordlund told AFP that the girl has "returned to Sweden with her family".
She landed Thursday in Stockholm with her parents, who had travelled to Iraq several times over the past eight months to try to bring her home, according to regional newspaper Boras Tidning.
Police said her boyfriend, a Moroccan who reached Sweden as an unaccompanied minor three years ago, was dead.
Kurdish forces rescued the girl near Mosul on February 17, according to a statement from the Kurdistan Regional Security Council.
Nordlund would not reveal many details of the rescue, though she confirmed it was the result of "collaboration between the Swedish authorities and foreign governments".
In an interview broadcast this week by TV channel Kurdistan 24, the Swede said in broken English that she met her boyfriend in 2014 and that he became radicalised after watching IS videos.
"Then he said he wanted to go to ISIS and I said, 'Okay, no problem,' because I did not know what ISIS meant or what Islam was," she said.
She was pregnant when they left Sweden in May 2015, taking trains and buses across Europe until they finally crossed the border from Turkey to Syria. They were then driven by IS jihadists to Mosul.
"In my house we had nothing, no electricity, no water, nothing. It was totally different from how I lived in Sweden, because in Sweden we have everything, and when I was there I did not have anything, did not have any money either. It was a very hard life," she said.
"When I got a phone, I started to contact my mum and I said I wanted to go home. She contacted the Swedish authorities," she told Kurdistan 24.
Swedish media has published desperate text messages she sent her mother while she was stuck in Iraq.
"I'm going to die in a bombing or they're going to beat me to death or I'm going to kill myself mum, really, I don't have the strength to go on," she wrote.
According to Swedish media reports, she gave birth to a son in Iraq. She and the child returned to Sweden together.
Her boyfriend, a Moroccan, had come to Sweden alone in August 2013 at the age of 17.
"He had been a suspect in a burglary in Stockholm," Ulf Hoffmann, a police investigator, told AFP.
The young man, also suspected of drug crimes, was dead, Hoffmann added, without specifying how he had died.
In a video posted on the Internet, undated and unauthenticated, the man gives his age (born July 8, 1996) before declaring his hatred of the Swedish "racists".
The man's flight from Sweden coincided with an international arrest warrant issued against him.
IS took over Mosul and other parts of Iraq in a lightning offensive in 2014. It has since suffered several setbacks as it faces Iraqi and Kurdish forces on the ground, as well as a US-led aerial campaign.
The jihadists also control large swathes of Syria.
The group has committed horrific abuses against the civilian population in both war-torn countries, including kidnapping and forcing thousands of women and girls into sexual slavery.