However, there were conflicting reports of how many had been killed in the blast, with one witness saying there were up to six casualties, and officials saying one soldier and the bomber had died.
Shebab spokesman Abdiaziz Abu Musab boasted that the bomb -- the latest in a long line of guerrilla style attacks they have carried out in the capital -- was part of a "renewed campaign of attacks against Western puppets in Somalia."
"Mujahedeen units carried out an operation targeting senior apostate intelligence officials," he said.
Somali Information Minister Abdulahi Ilmoge Hersi said the bomber was a former Shebab fighter who had joined the government's intelligence service.
"One Somali soldier and the bomber died and three others were injured in the explosion," he told reporters.
Abdukadir Ali, a Somali military official, had earlier said he had seen the bodies of six people, describing a scene of "chaos, smoke and pieces of human flesh" after the explosion.
Prime Minister Abdi Farah Shirdon Said was in his office at the time when the attacker struck, officials said, but he was not harmed by the blast.
"The area was closed down by the security forces... I saw several dead soldiers and others injured being rushed to hospital," said Mohamed Hussein, a witness.
The insurgents have vowed to topple newly elected President Hassan Sheikh Mohamud, who took office in September after being chosen by the country's new parliament, bringing an end to eight years of transitional rule.
Shebab fighters are on the back foot, having fled a string of key towns ahead of a 17,000-strong African Union force, which is fighting alongside Somali government troops to wrest territory off the Islamists.
Ethiopian troops are also battling the Shebab in the southwest of Somalia.
But the Shebab remain a potent threat, still controlling rural areas as well as carrying out guerrilla attacks -- including suicide bombings -- in areas apparently under government control.
War-ravaged Mogadishu was hit last year by a string of bloody bomb attacks, although it has been calmer in recent weeks.
A car bomb in December killed three people, while in November a suicide bomber attacked a restaurant.
Somaliland dismisses UK’s warning
Meanwhile, Somalia’s breakaway Somaliland enclave has dismissed as “baseless” a British government warning of an imminent attack on foreigners, Reuters reported.
London urged all British nationals to leave Somaliland immediately on Sunday, warning of a “specific threat” to foreigners. Ireland issued the same alert to its citizens.
Somaliland’s Foreign Minster Mohamed Abdillahi Omer told reporters on Sunday night that there was no “imminent threat and danger from terrorists” and that the “UK government’s presumption of insecurity in Somaliland is baseless.”
Even before its latest warning, Britain advised against all travel to Somaliland - an enclave that declared independence in 1991 - as well as to wider Somalia, due to the “high threat from terrorism” and kidnapping. On Sunday it said there was an ongoing danger of “kidnapping for financial or political gain, motivated by criminality or terrorism.”