Dozens of people who had been taken hostage in the stand-off at the Hayat Hotel in Mogadishu were freed.
Elite armed forces battled the militants for 30 hours from Friday evening after the attackers blasted and shot their way into the hotel, which is popular with lawmakers and other government officials.
"We have confirmed so far 21 dead people and 117 others injured," Ali Haji, the minister for health, told national broadcaster SNTV.
Three attackers were shot dead during the military operation to end the siege, said Hassan, a police captain who only gave one name.
A fourth one was shot dead in the area on Sunday morning as he attempted to melt into the civilian population, Hassan said, adding that the exact number of attackers was still unclear.
The al Qaeda-linked al Shabaab group, which has been fighting to topple the government in the Horn of Africa country for more than a decade, claimed responsibility for the attack.
Friday's attack at the Hayat was the first major incident since President Hassan Sheikh Mohamud took office in May.
The attackers shot and killed civilians who fled towards the wall of the hotel compound and its blown-up gate after they struck, said a police captain who gave only the name Ahmed, adding that they killed 10 security personnel with firearms and grenades.