At least four Pakistani soldiers were killed in an attack by militants in northwestern Pakistan near the border with Afghanistan early on Thursday, the military said.

The Tehrik-e-Taliban Pakistan (TTP), or Pakistan Taliban, which renewed an allegiance with the Taliban when the Islamist militant group took power in Afghanistan last August, claimed responsibility for the attack.

The militants tried to infiltrate Pakistan from Afghanistan in Hassan Khel area of North Waziristan district, Pakistani troops responded and foiled the attempt, the army said.

"Four soldiers were killed during the intense exchange of fire with the militants," the military's media wing said in a statement on Thursday night.

Militants also suffered heavy casualties, the statement said, quoting intelligence reports, but independent confirmation was not immediately possible because the districts along the mountainous Afghan border are off limits to journalists and human rights organisations.

Last month, at least five Pakistani soldiers were killed at a border post in northwestern Kurram district in a cross-border militant attack from Afghanistan.