PESHAWAR, Pakistan (Reuters) - Four heavily-armed suicide bombers attacked a military facility in northwestern Pakistan on Saturday and killed two soldiers, but failed to storm a busy mosque inside, the army said.

A splinter group of the Pakistani Taliban, Tehreek-e-Taliban Pakistan Jamaat-ur-Ahrar (TTP-JA), claimed responsibility for the attack on Ghalani Camp in Mohmand Agency that is part of the lawless Federally Administered Tribal Areas (FATA) bordering Afghanistan.

A spokesman for TTP-JA, Ehsanullah Ehsan, said the Islamist group carried out the raid in an effort to set free fellow militants from custody of the security forces.

The militants began their attack at 6 am local time (0100 GMT) and attempted to storm into a mosque where a large number of army recruits were present, the army said.

“Wearing suicide jackets they opened fire and tried to rush inside the mosque. They were taken on and contained in the outer courtyard of the mosque,” the military said in a statement.

Two of the attackers were shot dead and two detonated their explosive vests, preventing a “huge disaster”, the army added.

Pakistan’s frontier regions, deeply conservative and hard to access due to rough terrain, have long been the sanctuary of fighters from al Qaeda, the Taliban and other militant groups.

In 2014, the army launched a major operation in other parts of FATA including North and South Waziristan against insurgents who routinely attacked government officials and civilians.

Most of the myriad militant groups that stage attacks inside Pakistan seek to overthrow the government to establish an Islamic theocracy and impose a stricter interpretation of the religion than is practised in much of the country.