The Swedish Rescue Services Agency (Räddningsverket) learned of the workers’ release on Saturday afternoon.
“The two employees who were captured or in some way kidnapped in Somalia are now free,” agency spokesman Åke Svensson told the TT news agency.
Räddningsverket had also received confirmation from the UNDP that both men would be immediately flown out of the war-torn country.
Local residents and aid workers said the insurgents attacked the town of Hodur, 370 kilometres (235 miles) west of the Somali capital Mogadishu, around 4:30 am (0130 GMT) but then withdrew.
“The two UN mine action workers were taken from the IMC (International Medical Corps) compound. We don’t know their whereabouts and they were taken after Islamists took control of the town,” a UN official told AFP.
First reports said the two foreigners abducted were Swedes, but it later emerged that one was Danish and the other Swedish.
Prior to the release, a UN security official in Nairobi said: “Local authorities in the zone are negotiating with the kidnappers and calling for their immediate and unconditional release. Local chiefs are also involved in the negotiations.”
Local resident Hassan Mohamed told AFP the militants wounded a district commander during a a heavy exchange of gunfire in which a bodyguard was killed.
“They took control of the town and they also raided the IMC compound and took two foreign aid workers,” he added.
According to another local resident and a humanitarian worker the Islamists also snatched a Somali.
“The attack took place in two locations: the governor’s compound and the district commissioner’s house. Now they have left the town but they took two foreign aid workers and a Somali national,” resident Mohamed Abdullahi Yare told AFP.
The latest abductions take the number of humanitarian workers kidnapped recently in this Horn of Africa country to nine: two Swedes, two Italians, one British national, one Kenyan and three Somalis.
An uninterrupted civil war has plagued Somalia since the 1991 overthrow of president Mohamed Siad Barre, defying numerous peace initiatives and truce deals.
Since their ouster early last year by joint Ethiopian and Somali forces, the Islamists have waged a guerrilla war, which according to international rights groups and aid agencies has left at least 6,000 civilians dead and displaced hundreds of thousands.
The African Union has deployed 2,600 peacekeepers in Somalia — short of the pledged 8,000 troops — but they have failed to stem violence.