Islamist militants killed 19 people in an attack on a top hotel in the capital of Mali before Malian commandos stormed the building and rescued 170 people, many of them foreigners.
Two militants were killed.
President Ibrahim Boubacar Keita announced the death toll and said seven people were wounded in the attack, which has been claimed by jihadist group Al Mourabitoun and al Qaeda in the Islamic Maghreb (AQIM).
Friday's assault on the Radisson Blu hotel comes a week after deadly Islamic State attacks in Paris that killed 130 people. Tha Mali attack was the latest in a series of deadly raids this year on high-profile targets in the country, which has battled Islamist rebels based in its desert north for years.
"Tonight the death toll is heavy," Keita said on state television, declaring a 10-day state of emergency and three days of national mourning. The president, who cut a short visit to a regional summit in Chad, said two militants also died.
In a speech on the sidelines of a summit with Asian nations in Malaysia, U.S. President Barack Obama described the raid in Mali as "another awful reminder of the scourge of terrorism".
"Once again, this barbarity only stiffens our resolve to meet this challenge," he said. "We will stand with the people of Mali as they work to rid their country of terrorists and strengthen their democracy. With allies and partners, the United States will be relentless."
The attack is a sharp setback for former colonial power France, which has stationed 3,500 troops in northern Mali to try to restore stability after a rebellion in 2012 by ethnic Tuaregs that was later hijacked by jihadists linked to al Qaeda.
It also puts a spotlight on veteran militant leader Mokhtar Belmokhtar, whose group Al Mourabitoun staged the attack months after he was reported killed in an air strike.
Minister of Internal Security Colonel Salif Traoré said the gunmen burst through a hotel security barrier at 7 a.m. (0700 GMT), spraying the area with gunfire and shouting "Allahu Akbar", or "God is great" in Arabic.
"At first I thought it was a carjacking. Then they killed two guards in front of me and shot another man in the stomach and wounded him and I knew it was something more," said Modi Coulibaly, a Malian legal expert who saw the assault start.
The attack ended around 4 p.m. In an earlier report, a U.N. official said U.N. peacekeepers searching the hotel made a preliminary count of 27 bodies.
As troops stormed the hotel, state television showed them brandishing AK47s in the lobby. A body lay under a brown blanket at the bottom of a flight of stairs.
The U.S. State Department said one American was killed. The White House said it was working to locate all Americans in Mali, and it offered to help with an investigation and urged its citizens to limit their movements around Bamako.
A man who worked for a Belgian regional parliament was also among the dead, the assembly said. France's Defence Minister Jean-Yves Le Drian said he was not aware of any French nationals killed.
BURSTS OF GUNFIRE
Bursts of gunfire were heard as the militants went through the hotel room by room and floor by floor, one senior security source and a witness told Reuters.
Some people were freed by the attackers after showing they could recite verses from the Koran, while others managed to escape or were brought out by security forces.
One of the rescued hostages, celebrated Guinean singer Sékouba "Bambino" Diabate, said he had overheard two of the assailants speaking English as they searched an adjacent room.
"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," Diabate told Reuters by phone.
"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'."
Twelve Air France flight crew members were in the hotel but all were brought out 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 Chinese citizens had been killed in the attack.
POISSIBLY ONLY TWO ATTACKERS
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 violence has continued in Bamako and central Mali on the southern reaches of the Sahara.
One security source said as many as 10 gunmen had stormed the building, although the company that runs the hotel, Rezidor Group, said it understood there were only two attackers.
Al Mourabitoun has claimed responsibility for a number of attacks, including an assault on a hotel in the town of Sevare, 600 km (375 miles) northeast of Bamako, in August in which 17 people including five U.N. staff were killed.
One of its leaders is Belmokhtar, blamed for a large-scale assault on an Algerian gas field in 2013 and a major figure in insurgencies across North Africa.
In the wake of last week's Paris attacks, an Islamic State 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, who was contacted online by Reuters. "The bitterness from Mali, the arrogance of the French, will not be forgotten at all."