The attack began with two suicide car bombs at around 5 a.m. (0200 GMT), followed by hours of heavy fighting, Ahmed Hassan, a military officer in the nearby town of Bahdo, told Reuters.
"Al Shabab torched the telecommunication station of the town, and so it is off the air now. We know we killed five al Shabaab fighters," Hassan said.
One car bomb hit a military truck guarding the base entrance, while the other was blown up outside, he said.
In a statement, al Shabab spokesperson Abdiasis Abu Musab said the group launched the assault in Qayib using suicide car bombs before its fighters attacked from different directions. The fighters killed several soldiers and stole weapons and military vehicles, Abu Musab said.
It wasn't immediately clear how many people had been killed in total, but at least nine soldiers were injured, the military's Hassan said.
Government forces, supported by clan militias, have made a number of battlefield gains against al Shabab in the last three months, regaining territory long held by the group..
The al Qaeda-linked group has killed tens of thousands of people since 2006 in its fight to overthrow Somalia's Western-backed central government and implement its interpretation of Islamic law.