Kenyan security forces have killed all the Somali militants who stormed an upscale Nairobi hotel compound, taking at least 21 lives and forcing hundreds of people into terrifying escapes, the government said on Wednesday.

Fifty people believed to have been in the complex remained unaccounted for on Wednesday afternoon, the Kenya Red Cross said, raising the possibility of a much higher final death toll.

The bloody bodies of five attackers were broadcast across social media as President Uhuru Kenyatta announced the end of a 20-hour overnight siege that echoed a 2013 assault that killed 67 people in the Westgate shopping centre in the same district.

Sixteen Kenyans — including a policeman — an American 9/11 survivor, and a British development worker were among the casualties, said police Chief Joseph Boinnet.

Somali group al-Shabaab, an al-Qaeda affiliate fighting to impose strict Islamic law, said they carried out the attack in revenge for the U.S. decision to recognize Jerusalem as Israel's capital.

"The security operation ... is over and all the terrorists eliminated," Kenyatta said in a televised national address, looking drained and grave.

Kenyatta did not specify how many assailants there were, but CCTV clips showed at least five dressed in black, some sporting green grenade belts.

In one clip, one militant is seen waiting outside a restaurant before blowing himself up in a cloud of debris just after 3 p.m. local time on Tuesday. Another explosion near the entrance gate, possibly a grenade, ignites three cars before four men stroll by firing assault rifles and split into two groups.

One group entered a nearby office building, where they left a grenade in the lobby, a private security professional present during the attack told Reuters. Then they shot into elevators and offices as they searched for victims up to the sixth floor.

The other group raked the restaurant with gunfire. Eventually the militants holed up on or near the top floor of the hotel, taking potshots at those fleeing, he said.

Deadly crossfire seemingly avoided

Some victims had been dining in the Secret Garden restaurant and lay slumped at bloodstained tables, video seen by Reuters showed.

In its two-page statement claiming responsibility, al-Shabaab said: "This operation ... [was] a response to the witless remarks of U.S. president, Donald Trump, and his declaration."

Airstrikes against the group have intensified under Trump, but Tuesday's attack showed that the insurgents retain the ability to strike outside Somalia's borders.

Neighbouring Kenya, a hub for multinationals and the United Nations, became a frequent target for al-Shabaab after Kenya sent troops into Somalia in 2011 to try to create a buffer zone along its long, porous border.

The dusitD2 complex is home to offices of international companies including Colgate Palmolive, Reckitt Benckiser, Pernod Ricard, Dow Chemical ad SAP, as well as the dusitD2 hotel, part of Thai group Dusit Thani.

Ken Njoroge, chief executive of a company in the DustiD2 complex that offers mobile banking services, said he was unable to locate several employees. "It's very difficult for the families because the passage of time only makes the problem bigger," he said.

Hiram Macharia, marketing executive at LG Electronics, said he grabbed a fire extinguisher and headed downstairs after the first blast.

"Then we saw two of the attackers firing at the elevators and we turned back," he told Reuters outside the hotel. "They were firing twice at each of the elevator doors and the two staircase doors on each floor as they walked up the building. One of them fired at our office doors, entered slightly and then moved on."

Macharia and some colleagues hid under desks, he said, but one person ran to the roof and was shot dead there.

An injured woman receives medical attention at the scene of an explosion at a hotel complex in a Nairobi suburb. (Luis Tato/AFP/Getty Images)

Mamadou Dia was on a business trip from Paris for STP Consultants when he ended up huddled in a room with Chinese and Canadian hotel guests and a waiter.

"They told us by text that the police knew we were in that room and that they would come, and one-and-a-half hours later, the police came to evacuate us," he said.

As armed officers escorted them out, the attackers shot at them "like snipers," he said, adding that police fired back.

Al-Shabaab has vowed retribution against Kenya for sending troops to Somalia to fight it since 2011 and has killed hundreds of Kenyans. Their deadliest assault was on Garissa University in 2015, which left 147 dead.

Security forces appeared to have avoided some of the mistakes made during the 2013 Westgate attack, when police and soldiers shot at each other, then soldiers looted the mall. Tuesday's violence came a day after a magistrate ruled that three men must stand trial in connection with the Westgate Mall siege.

British, U.S. victims ID'd

The British aid group Gatsby said its Africa programs director was among those killed in the attack. A statement said the group was "shocked and saddened" to confirm the death of Luke Potter, and that he had "devoted the past 10 years of his career to helping some of the poorest and most vulnerable people in the world."

Kenyan security forces help people escape from the complex. Al-Qaeda-linked al-Shabaab claimed responsibility for the attack. (Kabir Dhanji/AFP/Getty Images)

San Francisco-based I-DEV International confirmed that the American killed was Jason Spindler, the company's co-founder and managing director.

Spindler's father, Joseph, says his son grew up in Houston, graduated from the University of Texas and was enjoying a successful career on Wall Street when he decided that he wanted to develop a model for helping low-income people.

Spindler later became a founder of I-DEV and had been living in Kenya for about five years. His father said he worked with international companies to form business partnerships in Kenya that would boost local economies.

Meanwhile, burials were taking place in Kenya for several of the deceased, including for Feisal Ahmed, who was killed with his colleague Abdalla Dahir.

The pair worked for the Somalia Stability Fund, managed by the London-based company Adam Smith International.

At a Chiromo morgue, the family of a 35-year-old man collapsed upon hearing a body had arrived with his identification papers.

"He is gone, he is gone," the father repeated into his phone as his mother wrapped a shawl around herself and wept.