The official also said that local tribal chiefs were also involved in the talks.
"The Danish and the Swedish workers were released ... without ransom and they are safe waiting for a flight to Nairobi," said Mohamed Nur Salad, a UN security and safety worker.
"We have spoken with them and they are well, although they are still in a bit of a state of shock," said Karin Viklunda, a Swedish government spokeswoman in Stockholm.
Local residents and aid workers said the fighters attacked the town around 4.30am (0130 GMT) but then withdrew.
Hassan Mohamed, a local resident, said the fighters wounded a district commander during an 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 said.
The Dane and the Swede were working for Raddningsverket, a risk assessment and emergency management agency funded by the Swedish government.
They were carrying out a demining training assignment for the United Nations Development Programme (UNDP).
Saturday's abductions are the latest in a series involving humanitarian workers. The fate of six aid workers - two Italians, one British national, a Kenyan and three Somalis - kidnapped in April remains unknown.
Aid groups have scaled down operations in Somalia due increased insecurity, largely blamed on Islamist fighters who have waged a war since they were ousted by joint Somali-Ethiopian forces in early 2007.
The African Union has deployed 2,600 peacekeepers in Somalia - well short of a promised 8,000 troops - but so far has failed to stem the violence and unrest.
At least 2.6 million Somalis are facing hunger due to acute food shortages spurred by a prolonged drought, insecurity and high inflation.
The UN famine monitors have warned that the figure could reach 3.5 million by the end of the year.