ALSO READPakistan must end tolerance for Afghan Taliban groups: US Afghanistan-Pakistan border remains a sanctuary for Taliban, Haqqani network: Pentagon Afghanistan: Protests break out against Pakistan's support to Taliban German counsulate attack: Death toll rises to six; at least 100 wounded in Afghanistan Taliban wouldn't last a month without Pakistan's support: Afghan President
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.
"In total, there were 10 attackers involved in the attack on the Afghan army corps. Seven of them were killed, two blew themselves up, and one was detained by Afghan forces," Waziri told AFP.
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.