Special forces staged a dramatic floor-by-floor rescue at the Radisson Blu hotel in Bamako, according to local television and security sources, to end the nine-hour siege.
The assault, which France has said was likely masterminded by notorious Algerian militant Mokhtar Belmokhtar, added to fears over the global jihadist threat a week after the Paris massacre that left 130 people dead.
Malian television broadcast chaotic scenes from inside the hotel as police and other security personnel ushered bewildered guests along corridors and across the main lobby.
A UN official said 12 bodies were found in the basement of the Radisson Blu Hotel in Bamako, with another 15 bodies discovered on the second floor. It is not clear if the figure of 27 also included any attackers involved in the incident.
Another UN official, UN Mali mission spokesman Olivier Salgado, said two “extremists” had been killed. He added that forces were going from room to room, checking for more casualties.
Islamic extremists armed with guns and grenades stormed the hotel in the west African country’s capital, with security forces later freeing guests floor by floor.
US and French special operations forces assisted Malian troops in the response to the attack by an as-yet unknown number of gunmen.
An extremist group led by former al Qaeda commander Moktar Belmoktar claimed responsibility for the siege.
A Malian military official initially said there were 10 gunmen, but later in the day it became unclear how many assailants had taken part.
At least six Americans were evacuated from the hotel.
About 40 members of the French special police forces played a supporting role, France’s national gendarme service said.
Around 170 guests and employees were initially taken hostage, but some apparently escaped in the initial chaos or hid in the sprawling hotel, which has 190 rooms and a spa, outdoor pool, and ballroom.
“It was more like a real terrorist attack,” said Mr Salgado. “The intention was clearly to kill, not to necessarily have people being (taken) hostage.”
At least one guest reported the attackers instructed him to recite verses from the Koran before he was allowed to leave.
The guests included visitors from France, Belgium, Germany, China, India, Canada, the Ivory Coast, and Turkey. But the attack was perceived by many in France, particularly in the government, as a new attack on French interests.
An extremist group, that two years ago split from al Qaeda’s North Africa branch and is led by Moktar Belmoktar, claimed responsibility for the attack in a recorded statement carried by Al-Jazeera. The group said it wanted fighters freed from Mali’s prisons and for attacks against northern Malians to stop.
The jihadist group, known as the Mourabitounes, was formed in 2013 after Belmoktar left al-Qaeda in the Islamic Maghreb and fused with a Malian militant group. The statement claimed the Mourabitounes had attacked in coordination with the “Sahara Emirate” affiliated with al Qaeda.
The French military operation in Mali in 2013 against Islamic extremists who were holding the northern half of the country was the first of several foreign interventions that François Hollande has launched as president. Those interventions prompted increased threats against France and French interests from Islamic extremist groups, from al Qaeda’s North African arm to IS.
“We should yet again stand firm and show our solidarity with a friendly country, Mali,” Hollande said.
The gunmen had stormed the hotel shouting “God is great,” in Arabic before firing on the guards. An employee said the militants had also used grenades.
Monique Kouame Affoue Ekonde, from Ivory Coast, said she and six other people were escorted out by security forces . She said she was “in a state of shock”.