A senior figure in the Somali militant group al-Shabab has told the BBC it carried out a deadly attack on a shopping centre in neighbouring Kenya.
The gunmen have been cornered but an unknown number of hostages are still trapped inside Nairobi's Westgate shopping mall, officials say.
Kenyan President Uhuru Kenyatta said 39 people had been killed, including some of his family, and 150 were injured.
Al-Shabab said the attack was in response to Kenyan troops in Somalia.
There are about 4,000 Kenyan troops in the south of Somalia, where they have been fighting the militants since 2011.
On its Twitter feed, al-Shabab - which has links to al-Qaeda - said it was behind what it called the "Westgate spectacle".
In his TV address, Mr Kenyatta said security forces were "in the process of neutralising the attackers and securing the mall".
He went on: "We shall hunt down the perpetrators wherever they run to. We shall get to them and we shall punish them for this heinous crime."
He said he had "personally lost family members in the Westgate attack".
As night fell in Nairobi, two contingents of army special forces troops are reported to have moved inside the mall.
A police officer inside the shopping centre told Reuters that the remaining gunmen were barricaded inside a Nakumatt supermarket, one of Kenya's biggest chains.
In the early hours of Sunday morning, the Kenya National Disaster Operation Centre said the number of hostages was "still unknown, but they are in several locations".
"The gunmen have been contained in one location, but there are hostages elsewhere in the vicinity who cannot access the exit," the centre said on its Twitter feed.
Upper levels of the mall had been secured, it said.
The attackers entered the Westgate centre at about 12:00 local time (09:00 GMT), throwing grenades and firing automatic weapons. A children's day event was being held at the time.
The BBC's security correspondent Frank Gardner says a security source told him that at least one of the attackers was a woman who appeared to have some kind of leadership role.
Our correspondent says that it is unclear how many civilians are hostages and how many are in hiding.
Al-Shabab said on Twitter that the Kenyan government wanted to negotiate an end to the Westgate attack. But officials have told the BBC they intend to hunt the gunmen down.
One gunman was arrested and died of his wounds, Kenyan officials told the BBC. Four other gunmen were arrested.
Security experts are reported to have long warned that the complex, which is at least in part Israeli-owned, was in danger of being subjected to a terror attack.
Some witnesses said the gunmen had told Muslims to leave and said non-Muslims would be targeted.
"They came and said: 'If you are Muslim, stand up. We've come to rescue you," said Elijah Lamau.
He said the Muslims left with their hands up, and then the gunmen shot two people.
The correspondent in Nairobi for the Economist, Daniel Howden told the BBC he spoke to one man with a Christian first name but a Muslim-sounding surname who managed to escape the attackers by putting his thumb over his first name on his ID.
However, the man told Mr Howden that an Indian man standing next to him who was asked for the name of the Prophet Muhammad's mother was shot dead when he was unable to answer.
The BBC's Mark Doyle, who is embedded with the African Union (AU) mission in Somalia, says the AU troops attack al-Shabab where they can.
Al-Shabab says the AU forces are invaders stopping their legitimate vision of creating an Islamic state and respond by mounting hit-and-run attacks, our correspondent says.
British Foreign Secretary William Hague said there would "undoubtedly" be British nationals caught up in Saturday's events.
The US State Department says it has reports that American citizens were injured in what it called "a senseless act of violence".
Two French citizens were among the victims of the attack, the Elysee Palace said in a statement.
President Francois Hollande "condemns the cowardly attack in the strongest terms and shares the pain of the family of our compatriots", it said.
Two Canadians, including a diplomat, are also among the dead.
Nairobi's mortuary superintendent, Sammy Nyongesa Jacob, told Reuters that Africans, Asians and Caucasians were among the bodies brought to the mortuary.
This is one of the worst incidents in Kenya since the attack on the US embassy in August 1998.