At least three mortars slammed into a nearby residential area during the ceremony for President Mohamed Abdullahi Mohamed, also known as Farmajo, Captain Mohamed Hussein said.
The explosions could be heard near the end of the handover ceremony, startling those attending.
The SITE Intelligence Group, which monitors jihadi websites, cited al-Shabab posts on Twitter that claimed responsibility for the attack.
"They don't care who gets hit but just want to terrorize everyone," said Mulki Ahmed, a neighbor.
More From This Section
This was the first mortar attack in the capital since Mohamed, who also holds US citizenship, was elected February 8 to lead a country recovering from a quarter-century of conflict.
"We can't achieve everything within a few months, therefore I am calling people to support us to serve our country," Mohamed said at today's ceremony.
While the al Qaida-linked al-Shabab has been pushed out of most of its key strongholds, it continues to carry out deadly attacks in the capital, Mogadishu.
Fears of al-Shabab attacks were a factor in delaying the presidential election multiple times since last year. On the eve of the election, two mortar rounds fired by suspected extremists hit near the voting venue, a heavily guarded former air force base.
The election of Mohamed and the peaceful transfer of power have been seen as a key step toward having Somalia's first fully functioning central government since 1991, when warlords ousted dictator Siad Barre and then turned on each other.