MOGADISHU, Somalia — Militants attacked two hotels in Mogadishu, Somalia’s capital, with grenades, bombs and heavy gunfire on Friday in what appeared to have been coordinated attacks, witnesses and officials said. At least 18 of the attackers were killed and at least six civilians were wounded, officials said.

The Shabab, an Islamist militant group that is affiliated with Al Qaeda, claimed responsibility for both attacks, saying that they were targeting government officials who were staying at the two sites, the Weheliye and Siyad hotels.

The attacks shocked residents of Mogadishu, who were breaking the daily Ramadan fast on Friday evening around the time of the attacks.

“I heard several loud explosions in several locations when I was prepared to break the fast,” said Ahmed Osman, a resident of the Howlwadaag neighborhood in the capital. “This is one of the terrifying moments in my life.”