Pakistan's military and officials say a group of militants have attacked a mosque at an army facility in northwest Pakistan, triggering a shootout in which four insurgents and two soldiers were killed.

The army in a statement said 14 troops were also wounded in Saturday's suicide attack on Ghalani Camp in Mohmand tribal region bordering Afghanistan.

It said the attackers wanted to enter the camp and started firing after they reached the mosque, where residents and a large number of recruits were present. It said the attackers were "contained in the outer courtyard" of the mosque, and subsequently all four died.

Two security officials also confirmed the account and said a search operation is underway to trace and arrest any accomplices of the attackers.