ISLAMABAD (Reuters) - Militants have crossed into Pakistan from Afghanistan and killed a Pakistani army officer and a soldier, the Pakistani military said on Monday.

The uneasy neighbors, both of which are important U.S. allies, accuse each other of harboring militants on either side of the border, and their two armies have exchanged fire across it during periods of tension over recent years.

Four Pakistani soldiers were wounded in the attack on a military post in the northwestern region of Bajaur, the army said.

It did not say when the raid took place but said up to 10 of the attackers were believed to have been killed in Pakistani retaliatory fire.

Independent verification was not possible as the area is largely closed off to reporters.

Pakistan and Afghanistan share a rugged, porous border of 2,500 km (1,500 miles).

Afghanistan, which does not recognize the border, has lately been angry over a Pakistani plan to build a fence along most of it.

The Pakistani army said the absence of Afghan central government’s writ on its side of the border facilitated such militant attacks.

There was no immediate response from the Afghan government.

The attack came a week after unidentified gunmen shot and killed a Pakistani diplomat near his residence in the eastern Afghan city of Jalalabad.