MOGADISHU, Somalia — A suicide bomber walked into the mayor’s office in Mogadishu, Somalia’s capital, during a high-level security meeting on Wednesday and detonated explosives, seriously injuring the mayor and killing at least six people, according to local authorities.

The mayor, Abdirahman Omar Osman, was in critical condition with head and stomach injuries, officials said.

James Swan, an American diplomat who is the United Nations’ special representative for Somalia, had visited the mayor’s office earlier Wednesday but left before the attack, the authorities said.

The Shabab, an Islamist extremist group with links to Al Qaeda, claimed responsibility and said that Mr. Swan was the intended target, according to Radio Andalus, the group’s radio station. The Shabab, which seeks to overthrow Somalia’s Western-backed government, often target government offices and other high-profile places in Somalia’s capital.