MOGADISHU - Islamist militants attacked a hotel used by government officials in Somalia's capital Mogadishu on Sunday evening, police and witnesses said.

Al Qaeda-linked al Shabaab, which controls large swathes of the country, claimed responsibility for the attack, saying in a statement that it was targeting the nearby presidential palace.

"We were shaken by a huge blast, followed by heavy exchange of gunfire," said Ahmed Abdullahi, who lives close to the scene. "We are just indoors and listening to gunfire."

Security forces were responding to an attack by al Shabaab on a hotel in the capital's Bondhere district, state broadcaster SNTV said on Twitter.

"The forces rescued government officials and members of the public who were trapped in the building," the broadcaster said.

The assailants stormed the Villa Rose hotel, which is close to the presidential palace, two police officers told Reuters. It was not immediately clear how many attackers there were, the officers said.

Some government officials at the Villa Rose were rescued after using windows to escape, said Mohammed Abdi, one of the police officers.

The state minister for the environment, Adam Aw Hirsi, wrote on Twitter that he was safe after a "terrorist explosion targeted at my residence" at the hotel, where many government officials stay.

President Hassan Sheikh Mohamud, who was elected earlier this year, has been carrying out a military offensive against al Shabaab.

(Reporting by Abdiqani Hassan and Feisal Omar; Writing by Duncan Miriri; Editing by Hugh Lawson and David Evans)