The rampage in a heavily fortified area in the garrison city of Rawalpindi also brought the war home to the military brass as insurgents persist with brazen attacks despite several army offensives against them in northwestern regions bordering Afghanistan.
A military statement said four attackers hurled grenades, then opened fire as they rushed toward the mosque, which was located on Parade Lane in a military residential colony, just a few miles from the capital. Two suicide bombers then blew themselves up inside, while the other two militants were killed in an exchange of gunfire, it said.
The dead included a major general, a brigadier, two lieutenant colonels, one major and a retired major as well as three regular soldiers, military spokesman Maj. Gen. Athar Abbas said. Seventeen children and 10 civilians also were killed.
City residents said access to the mosque was mostly restricted to soldiers and their families.
Witnesses said two of the militants entered the mosque, which had up to 200 worshippers inside, while others ran into buildings nearby. With reporters prevented from getting close, security forces exchanged fire with the assailants for an hour before they blew themselves up or killing them.
Nasir Ali Sheikh saw the attackers at the mosque as he walked there to pray. He said they were dressed in traditional Pakistani clothing of loose pants and a long tunic and carried hand grenades, automatic weapons and ammunition belts slung around their shoulders.
"They were killing people like animals," he said. "I couldn't understand what was happening."
The mosque's walls and prayer mats were covered in blood and shattered glass lined the floor, TV footage showed.
The attack was the third in Rawalpindi in the last two months. In the most high-profile incident, a team of militants attacked the army headquarters on Oct. 10 and held dozens hostage in a 22-hour standoff that left nine militants and 14 other people dead.
Three helicopters hovered overhead while trucks carrying commando teams and ambulances raced through the cordoned-off area as soldiers with rifles ready kept onlookers and traffic away.
The attack began when several gunmen staged an explosion to break through a checkpoint close to the mosque, said Yasir Nawaz, a police official at the scene.
He said the installation included an army parade ground as well as the mosque, which was often used by military officers.
Abbas said authorities were investigating how the attackers penetrated the stringent security ring surrounding the area.
It was the second attack against a military installation this week. A suicide bomber also struck the entrance of the navy headquarters in Islamabad, killing two security guards on Wednesday.
Violence in nuclear-armed Pakistan has escalated since the army launched an offensive in mid-October against Taliban militants in the northwestern tribal area of South Waziristan near the Afghan border. Soldiers have pushed deep into what was a militant stronghold, but many insurgents appear to have fled.