ALSO READAfghan official: NATO drones kill 11 Islamic State fighters NATO airstrike kills 30 Afghan civilians, officials say 30 Afghan civilians killed in Kunduz NATO airstrike: officials Afghan military death rate rises in 2016: watchdog 'Shoddy leaders contribute to rising Afghan military deaths'
The attack lasted several hours but was over by early evening, according to the spokesman, who wished to remain anonymous.
US General John Nicholson, commander of NATO's Resolute Support operation, said in a separate statement that the attack targeted "soldiers at prayer in a mosque and others in a dining facility" of the 209th Corps of the Afghan army.
The general, who did not give a toll, praised "the Afghan commandos who brought today's atrocity to an end".
Afghan defence ministry spokesman General Dawlat Waziri said gunmen wearing Afghan army uniforms had launched the attack on the army compound on the outskirts of Mazar-i- Sharif, capital of Balkh province.
The Afghan general earlier gave a toll of eight soldiers killed and 11 wounded but said this would change.
Several military helicopters were hovering over the site and ambulances were evacuating the bodies of the victims, an AFP correspondent said.
In a statement the Taliban claimed responsibility for the operation.
The last major attack against a military site dates back to early March, a coordinated hours-long assault on the country's main military hospital in Kabul.
Officials said around 50 people were killed in that attack, though credible sources said it was more than double that.
That operation was claimed by the Islamic State group.
(This story has not been edited by Business Standard staff and is auto-generated from a syndicated feed.)