Fred Ngoga Gateretse, an official with the African Union, was having coffee at that cafe around noon when he heard two deafening blasts. He cowered on the floor and watched eight gunmen with scarves twisted over their faces firing at shoppers and then up at Kenyan police officers who were shooting down from a balcony as panicked shoppers dashed for cover. “Believe me, these guys were good shooters,” Mr. Gateretse said. “You could tell they were trained.”

Several witnesses said the attackers had shouted for Muslims to run away while they picked off other shoppers, executing them one by one. The mall, one of Nairobi’s most luxurious, with glass elevators and some of the most expensive shops in town, is also popular with expatriates. It has served as the place for a power lunch, to catch a movie, to bring children for ice cream.

Four Americans were believed to have been injured in the attack, American officials said, and none were reported killed. Secretary of State John Kerry, who called the attack “a heartbreaking reminder that there exists unspeakable evil in our world,” said the wife of a local employee of the American government was among the dead. Two Canadians, one of them a diplomat based in Nairobi, and two French citizens were killed, their governments said.

A confidential United Nations security report on Saturday described the attack as “a complex, two-pronged assault” with two squads of gunmen dashing into the mall from different floors at the same time and opening fire.

The Shabab, an Islamist militant group based in Somalia, took responsibility for the attack, saying it was revenge for Kenya’s military operations in Somalia, which began nearly two years ago. “Kenya will not get peace unless they pull their military out of Somalia,” said Ali Mohamoud Rage, the Shabab’s spokesman, in a radio address. The Shabab also sent out a barrage of buoyant Twitter messages, bragging about the prowess of their fighters before Twitter abruptly suspended the account late Saturday. Later, a new one was set up.