The attack on the tightly guarded headquarters in the city of Rawalpindi came as the army prepares a major offensive against Pakistani Taliban militants in their northwestern stronghold on the Afghan border.
The gunmen drove in a white van to a main gate at the sprawling complex, and opened fire and threw at least one grenade when challenged, security officials said.
The gunmen then exchanged fire with soldiers for about 40 minutes.
"The situation is under control ... all the gunmen have been killed", army spokesman Major-General Athar Abbas later told Geo Television.
Four guards at the gate's checkpost were killed, he said.
Published by i On Global Trends - Mike Hitchen Online - news, opinion, analysis
Putting principles before profits
Putting principles before profits