No one immediately claimed responsibility for the attack, but al-Qaida and Islamic State militants are active in southern Yemen.
The attackers, however, failed to storm the army command and the security headquarters.
Worst attack in country in 18 months; two foreign staff gunned down.
Officials say the militants took hostage another 55 troops during the surprise attack.
Al-Qaeda militants have killed 12 soldiers in Yemen in separate attacks over the last two days.
Al-Qaida has claimed responsibility for an attack that killed three senior Yemeni security officers this week.