A brazen Taliban attack on a Pakistani military base on the outskirts of the northwestern city of Peshawar killed 29 people on Friday, including 16 worshippers who were gunned down when the militants stormed a mosque inside the compound during prayers.

The attack triggered an hours-long firefight at the base and the Pakistani forces said they killed 13 of the attackers, though it was unclear how many were involved in the assault.

Apart from the 16 slain inside the mosque, 13 air force and army employees at the base were also killed by the militants, said army spokesman Gen. Asim Saleem Bajwa.

The attack was a major blow to Pakistan's military, which had stepped up operations against militants following a horrific Taliban attack last December at a Peshawar school that killed 150 people, mostly children. It also underscored the ability of the militants to stage spectacular attacks on targets linked to the country's military and government.

In Friday's assault, the attackers first stormed the guard room of the Badaber base, according to air force officials. The base was established in 1960s as an air force facility but has in recent years mostly been used as a residential place for air force employees and officers from Peshawar.

Bajwa said the militants entered the base from different directions in a two-pronged assault -- apparently one push targeted the mosque -- but that security forces quickly responded. He also said at least 29 people were wounded in the firefight.

He insisted the assault was quickly repulsed because of the timely and coordinated action by the security forces. Later, he told reporters at a news conference in Peshawar "the attackers came from Afghanistan," though he stressed he did not mean that the government in Kabul was behind the assault. He declined to provide more details on that claim. There was no immediate response from Afghanistan.

One of the wounded security officials, Mohammad Rizwan, said he was coming out of the mosque when he was hit by a bullet.

"I fell down and I saw some of the attackers, but I don't know what happened later, I fell unconscious," he said.

A wounded soldier, Akram Ullah, said from his hospital bed that he was inside the mosque and that he remembers seeing a gunman with a grenade come in.

Fayaz Hussain Chaudhry, father of slain army Capt. Asfand Yar, told the Dunya news TV station that his son gave his life for his country. "He fought at the front of the battle today and he killed terrorists," he said.

Shortly after the attack, a suspected U.S. drone strike hit a home in the South Waziristan tribal region, south of Peshawar, killing at least three  militants and wounding five, according to two Pakistani security officials who spoke on condition of anonymity because they were not authorized to talk to the media.

Prime Minister Nawaz Sharif and the country's powerful army chief, Gen. Raheel Sharif, rushed to Peshawar and attended some of the funerals of the victims. According to Muslim tradition, the deceased are buried as soon as possible.

Earlier, the army chief met with the security forces taking part in the clearing operation at the base and also visited a military hospital where doctors were treating soldiers wounded in the attack.

A spokesman for the Pakistani Taliban, Mohamad Khurasani, claimed responsibility for the attack. In a statement to the media, he said 14 Taliban fighters were involved in the assault. They offered "safe passage" to women and children after attacking the base, Khurasani said. He added that the Taliban "targeted" 50 security forces, without explaining what that meant.

Pakistani TV footage showed army helicopters hovering near the base, as police and troops surrounded the area.

Local police officer Shahid Khan Bangash said a large explosion was heard as the militants first tried to storm the base. "We are hearing that the attackers were armed with guns and rockets," he said. Bangash said the attackers threw grenades at the guard room but were unable to enter the main area of the base.

Later in the morning, he said the firing had stopped and a search operation for the militants who might still be hiding in the area was under way.

Friday's attack came a day after Pakistan reported the arrest a militant figure behind a recent failed attempt to target an air force facility in Kamra, also in the northwest of the country. Counter-terrorism officer Junaid Khan in the southern port city of Karachi, where the raid took place, identified the suspect as Umar Hayat and said he was being questioned.

On Thursday, the Pakistani police in Karachi also reported the arrest of another prominent suspect, Syed Sheaba Ahmad, a former air force pilot who allegedly helped finance al Qaeda's newly formed South Asian affiliate.

The Pakistan air force has been playing an important role in the fight against militants since June 2014, when the army launched the much-awaited operation in North Waziristan, a restive tribal area along the Afghanistan border. Peshawar is the capital of Khyber Pakhtunkhwa province, which borders the tribal area. The air force frequently target militant hideouts in the tribal area and elsewhere.

The army says it has killed more than 3,000 militants so far in the North Waziristan offensive. The region was once considered to be the headquarters of the Pakistani Taliban who have been targeting security forces and public places in an effort to topple the elected government to enforce harsher version of Islam.