Dozens of Turkish hostages who were seized by Islamic militants in Iraq have been freed and safely returned to Turkey, the prime minister, Ahmet Davutoglu, said on Saturday.

The 49 Turks, including diplomatic staff, were seized from the consulate on 11 June when the Islamic State group (Isis) overran Mosul, in Iraq, and stormed the Turkish consulate. The hostages included the consul general, Ozturk Yilmaz, other diplomats, children and special forces police.

During a visit to Baku, in Azerbaijan, Davutoglu said that the hostages were released early on Saturday and had arrived in Turkey. He was cutting his visit short to meet with the hostages in the province of Sanliurfa, near Turkey’s border with Syria.

He did not provide details on the circumstances of their release but said the hostages were freed through the intelligence agency’s “own methods” and that no operation was carried out. He thanked Turkey’s intelligence agency and the foreign ministry’s head official for their efforts.

Turkey had publicly resisted joining a coalition to defeat Isis, citing its 49 kidnapped citizens and the US had been careful not to push Turkey too hard as it tried to free the hostages.

The extremist group has beheaded two American journalists and a British aid worker, who were working in Syria, as payback for airstrikes that Washington has launched against them in Iraq.

“I am sharing a joyful news which as a nation we have been waiting for,” Davutoglu said. “After intense efforts that lasted days and weeks, in the early hours our citizens were handed over to us and we brought them back to our country.

“They have crossed into Turkey and I am on my way to see them.”

Thirty-two Turkish truck drivers who were also seized in Mosul in June; six were released a month later. Turkey did not provide information about their release.