The Somali militant group al-Shabab has said it carried out the attack.

On its Twitter feed, the al-Qaeda-linked group said it confirmed it was behind what it called the "Westgate spectacle".

Al-Shabab has carried out a string of attacks in Kenya since 2011, when Kenyan troops moved into southern Somalia to fight the militants there.

Some eyewitnesses told AFP news agency they had heard the gunmen speaking Arabic or Somali.

Other witnesses said Muslims were told to leave and that 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 US state department says it has reports that American citizens were injured in what it called "a senseless act of violence".

Trapped

At the scene

By David OkwembahBBC Africa, Nairobi

This is one of the worst incidents since Kenya first experienced a terrorist attack on the US embassy in Nairobi in August 1998. The timing is telling, coming just at the time when most families are out for lunch or shopping.

The mall is located in a wealthy area of Nairobi and is home to supermarkets, cafes, restaurants, banks as well as leading fashion stores from around the world.

Attacks on Nairobi shopping centres have been on the radar of terrorist groups for the past four years, with alerts always circulated among the expatriate and diplomatic community. Kenya's incursion into Somalia to fight al-Shabab militants intensified security.

The military has been called in to help police secure the building. The permanent secretary for internal security, Mutea Iringo, has not ruled out the attack being carried out by a terror group.

The attackers entered the Westgate centre at about 12:00 local time (09:00 GMT), throwing grenades and firing automatic weapons.

Dozens of shoppers fled; many were trapped inside. Some reports spoke of hostages but this is unconfirmed.

Arjen Westra, who was drinking coffee at the time of the attack, told the BBC he thought the cafe he was in was being targeted by the gunmen.

"I could hear the gunfire moving towards the main entrance of the shopping mall, so some people ran out of our cafe in a kind of panic, and quite a number just fell down as flat as possible on the ground."

Mr Iringo, permanent secretary in the Internal Security Ministry, said special forces were now "evacuating civilians and searching for the criminals".

Nairobi police chief Benson Kibue initially said the gunmen had been trying to rob a shop, but later called it "a terrorist attack".

Media playback is unsupported on your device

Media captionThe BBC's Anne Soy is ordered to lie down, as military vehicles move in

Targeted

The BBC's Anne Soy, who is at the scene, said there were reports one of the gunmen has been shot dead.

Some of the attackers were reportedly dressed in hijabs, and it is unclear whether they were men or women, our correspondent says.

Image copyrightAPImage caption
Emergency medical staff treated some of the wounded near the building's entrance

Image copyrightAPImage caption
Dozens of shoppers were given assistance in evacuating the area

Image copyrightAPImage caption
As darkness fell, security forces were still surrounding the building, with gunmen believed to be holed up inside

Security guards used shopping trolleys to wheel out several wounded children and at least one man.

"The gunmen tried to fire at my head but missed. There are definitely many casualties," Sudjar Singh, who works at the shopping centre, told AFP.

"I saw three of the attackers dressed in black and with covered faces and they were carrying heavy rifles," said another witness.

One Nairobi resident, Anupa, who lives near the scene, told the BBC: "I heard the exchange of fire when it happened, and I heard what I thought was a grenade, but I didn't go outside. The whole area is cordoned off."

The Westgate centre is often frequented by wealthy Kenyans and expatriates. Police have urged residents to stay away from the area.