A young boy was shot in the head by one of the kidnappers during the confrontation and was in a coma, said Pierce Gerety, the agency's Somalia representative, who is based in Nairobi.
Villagers became suspicious of the minivan and pick-up truck loaded with five foreigners and 10 gunmen as it passed through Farsaley, about 75 miles south-west of Mogadishu, at dusk on Thursday.
"They realised this was a kidnapping in progress," said Mr Gerety. "They shot at the kidnappers to stop them. One bullet went into the minivan. The kidnappers shot a kid."
The heavily armed villagers surrounded the van, and forced the gunmen to pass their weapons out the windows of their vehicles, Mr Gerety said, quoting one of the hostages. Police took the kidnappers into custody, he added.
The village is in an area served by foreign relief agencies.
The hostages spent the night in the UN minivan, and yesterday drove back to Mogadishu, the Somali capital.
The Unicef workers included a British security officer, William Condie, as well as a Nepalese, a Sudanese, an Indian, and the American head of the World Health Organisation in Somalia.
The five were taken by an armed gang led by a Somali who formerly rented a vehicle to Unicef, and was unhappy his contract had ended.
International aid organisations employed hundreds of Somalis in the early 1990s until the UN mission to Somalia pulled out a year ago. Armed groups have regularly taken hostages to exact what they feel are arrears in wages.