The jihadist group Al-Murabitoun has claimed that its forces carried out the attack on a hotel in the capital of Mali .
At least 27 people were killed in the capital Bamako after the Islamist gunmen seized the Radisson hotel with at least 170 people inside, many of them foreigners.
Minister of Internal Security, Colonel Salif Traor, said the gunmen had burst through a security barrier at 7am, spraying the area with gunfire and shouting ‘Allahu Akbar’, or ‘God is great’ in Arabic.
Occasional bursts of gunfire were heard as the assailants went through the seven-storey building, room-by-room and floor-by-floor, one senior security source and a witness said.
Some people were freed by the attackers after showing they could recite verses from the Koran, while others were brought out by security forces or managed to escape under their own steam.
A security source said the drama was over by early evening and two militants were dead.
"We the Murabitoun, with the participation of our brothers from Al-Qaeda in the Islam Maghreb, claim the hostage-taking operation at the Radisson hotel," said a man in a recording broadcast by Al-Jazeera television.
A UN official said peacekeepers on the scene had seen 27 bodies in a preliminary count.
After the hostages were freed, the gunmen held out against security forces, a security ministry spokesman said.
"The attackers no longer have hostages. They are dug in the upper floors. They are alone with the Malian special forces who are trying to dislodge them," spokesman Amadou Sangho said.
State television showed footage of troops in camouflage fatigues wielding AK47s in the lobby of the Radisson Blu, one of Bamako's smartest hotels and a favourite with foreigners.
The peacekeepers saw 12 dead bodies in the basement of the hotel and another 15 on the second floor, the UN official said on condition of anonymity.
A man working for a Belgian regional parliament was among the dead, the assembly said.
Al-Murabitoun, headed by Algerian militant Mokhtar Belmokhtar, was founded in 2013 and presents itself as the West African branch of Al-Qaeda.
The former French colony has been battling Islamist rebels for several years. France's defence minister said that the notorious Algerian militant Belmokhtar was "likely behind" the siege.
Jean-Yves Le Drian said several countries had long been searching for Belmokhtar.
"He is likely behind this attack although we are not completely certain of it," the minister said earlier today.
One of the rescued hostages, celebrated Guinean singer Skouba 'Bambino' Diabate, said he had overheard two of the assailants speaking in English as they searched the room next to his.
"We heard shots coming from the reception area. I didn't dare go out of my room because it felt like this wasn't just simple pistols - these were shots from military weapons," Mr Diabate said.
"The attackers went into the room next to mine. I stayed still, hidden under the bed, not making a noise," he said. "I heard them say in English 'Did you load it?', 'Let's go'."
The raid on the hotel, which lies just west of the city centre near government ministries and diplomatic offices, came a week after so-called Islamic State militants killed 130 people in Paris, raising fears that French nationals were being specifically targeted.
UN Secretary-General Ban Ki-moon condemned the "horrific terrorist attack", indicating the violence was aimed at destroying peace efforts.
A UN spokesman said "the secretary-general deplores any attempt to derail the implementation of the agreement" signed in June between rival factions.
Twelve Air France flight crew were in the building but all were extracted safely, the French national carrier said.
A Turkish official said five of seven Turkish Airlines staff had also managed to flee. The Chinese state news agency Xinhua said three of ten Chinese tourists caught inside had been rescued.
Malian President Ibrahim Boubacar Keita cut short a trip to a regional summit in Chad, his office said.
Northern Mali was occupied by Islamist fighters, some with links to al-Qaeda, for most of 2012.
They were driven out by a French-led military operation, but sporadic violence has continued in Mali's central belt on the southern reaches of the Sahara, and in Bamako.
One security source said as many as ten gunmen had stormed the building, although the company that runs the hotel, Rezidor Group, said it understood that there were only two attackers.
Ongoing incident in Central Bamako, Mali. Any Irish there should follow instructions of local authorities. Any concerns call DFAT 01-4082527— DFATravel (@dfatravel) November 20, 2015
The Irish Defence Forces said ten Irish personnel currently in Mali are "safe and accounted for".
Al Mourabitoun has claimed responsibility for a number of attacks in Mali, including an assault on a hotel in the town of Sevare, 600km northeast of Bamako, in August in which 17 people including five UN staff were killed.
Its leader Belmokhtar was blamed for a large-scale assault on an Algerian gas field in 2013 and has been a major figure in insurgencies across North Africa.
In the wake of last week's Paris attacks, an IS militant in Syria told Reuters the organisation viewed France's military intervention in Mali as another reason to attack France and French interests.
"This is just the beginning. We also haven't forgotten what happened in Mali," said the non-Syrian fighter.
"The bitterness from Mali, the arrogance of the French, will not be forgotten at all."