NAIROBI — An unseasonable thunderstorm was rolling through central Nairobi on ­Tuesday afternoon as al-Shabab militants approached the five-star DusitD2 hotel.

Charles Karugu was at the taxi stand where he works opposite the upscale hotel when a deafening explosion ripped through a nearby security checkpoint, he recalled.

Before Karugu could react, a bullet pierced his abdomen. A handful of fighters from al-Shabab, a Somali militant group, had just begun what would be a 19-hour siege of the hotel and its surrounding office buildings, killing at least 21 people.

Karugu, like so many others who were hit in the explosive early stages of the attack, was saved by a civilian. Closed-circuit video footage would later show the attackers firing at others as they entered the hotel. As Karugu crawled away, bleeding, a Kenyan journalist flagged down a motorcycle taxi that ferried him to a hospital.

In a morning address to the nation Wednesday, Kenyan President Uhuru Kenyatta declared the end of the ordeal, saying all the attackers had been “eliminated” and 700 civilians evacuated to safety. Up to the time of his announcement, gunshots had continued to ring out from the besieged buildings. Later on Wednesday, Kenya’s police chief said the death toll stood at 21.

As people streamed out of the complex over the course of the siege, they shared raw, nightmarish recollections of the terrifying hours they passed fearing for their lives.

Hundreds of guests at the DusitD2 hotel and workers in its adjoining office complex hid under desks, locked themselves in rooms or ran for cover as the attack unfolded Tuesday afternoon and evening.

The attackers, dressed in black, fired into offices and threw grenades down stairwells. Many witnesses recounted seeing body parts and blood strewn in normally quiet hallways.

The terror has scarred the hotel and corporate offices, and the resilient, fast-paced city of which they are a part. A cafe in the complex, called Secret Garden, was destroyed when one attacker detonated a suicide vest. Bodies lay slumped over tables still cluttered with plates and laptops, according to Associated Press photos taken after the attack. Shattered glass littered the parking lot where it fell in cascades.

By late afternoon Tuesday, elite squads of heavily armed Kenyan special forces had arrived and entered the complex alongside private security contractors who were tasked with extracting the employees of companies that had hired them.

Office by office and floor by floor of the hotel, groups of evacuees were escorted out by the armed forces. Many were deep in shock; some wept uncontrollably.

Okoth Obado, the managing director of a public relations firm, stopped to collect himself in a parking lot that the staffs of nearby hospitals had taken over as a triage center. He touched the top of his head and found plaster on it.

“You know, this must be from the wall. A bullet passed right over my head and hit the wall above me,” he said.

Gunfire and explosions were audible as night fell, occasionally prompting everyone, including the heavily armed security forces, to drop to the ground.

Just before 11 p.m., Kenya’s interior minister and police chief went on live TV to assure the nation that the siege was almost over and that all that was left was “mopping up.”

As they spoke, gunfire echoed through the night air. Reporters outside the complex heard it again at 3:43 a.m. and at 5:51 a.m.

In statements to international media Tuesday night, the al-Qaeda-aligned Islamist militant group al-Shabab asserted responsibility for the attack. The assault on the hotel and office complex echoed a September 2013 attack by al-Shabab at the Westgate shopping mall, just a mile away, in which fighters armed with automatic weapons killed 67.

On Wednesday, before Kenyatta’s announcement that the siege had ended, Nairobians came out in large numbers to donate blood. The city regained its regular buzz and congestion as its roughly 4 million people went back to work.

As with the Westgate attack, Tuesday’s assault seemed to be aimed at Kenyan professionals and foreigners. It also demonstrated al-Shabab’s ability to carry out attacks outside Somalia despite a dramatic increase in airstrikes against the group by the Kenyan air force and U.S. military.

In a two-page statement issued Wednesday, al-Shabab said the attack was “a response to the witless remarks of U.S. president, Donald Trump, and his declaration of Al-Quds [Jerusalem] as the capital of Israel.”

Kenyan police said in a statement that 16 Kenyans, one Briton, one American and three others of African descent made up the 21 killed. Details of some of the victims became known by Wednesday morning as colleagues and family members announced the deaths of co-workers and loved ones.

Among the dead was Jason Spindler, an American who was chief executive of a consulting firm that has its Africa headquarters in the complex that was attacked.

A Kenyan lawmaker, Fatuma Gedi, announced the deaths of two Kenyans of Somali descent, Feysal Rashid Haji and Abdalla Sheikh Mohamed Dahir.

“We will come out of this cowardly act stronger and more united as a nation,” Gedi wrote on Twitter.

Others posted happier news about friends and family members who had made it out alive.

Brian Kuira, a self-proclaimed “optimist who loves coffee and elephants,” took to Twitter to announce his own survival.

“We’ve been rescued from Dusit,” he wrote. “I have a new found respect for Kenyan cops, so professional as they evacuated us. Thank you all for the prayers.”

Charles Kuruga, the taxi driver who was shot in the abdomen, said he would go back to work as soon as he could.

“That’s how I make a living,” he said. “But this time, I will be going back there in prayer.”

Rael Ombuor contributed to this report.