The militants from Afghanistan fired at Manozangal and Mukha posts in Bajaur tribal agency, a security official said.
Two soldiers were killed and three others, including an officer, were injured in the attack, the official said.
Bajaur is one of the seven such districts bordering Afghanistan. It shares a 52-km border with Afghanistan's Kunar Province.
The militants are said to be loyal to top Taliban leader Maulana Fazlullah who is believed to be hiding in Kunar since 2009 after escaping a military action in Swat.
More From This Section
The attack had triggered hours-long gunbattle between security officials and militants.
Pakistan's foreign ministry condemned today's cross-border attack and asked Afghanistan to stop rebels from using its soil for launching attacks on Pakistan.
"Pakistan condemns these unprovoked attacks. This matter has been raised with the Afghan government in Kabul and with their Embassy in Islamabad," An FO spokesman said in a statement.
"It has been emphasised that Afghanistan must take concrete steps to stop the use of its territory against Pakistan," the ministry said.
Pakistani Army retaliated today's attack and killed several militants.
The three injured security officials were sent to a hospital and a search operation was launched in the area.
"The militants from Tehreek-e-Taliban Bajaur and Swat chapters had regrouped in Ghund village of Afghanistan and launched the attack from there," an official said.
This is the third incident of cross-border attack and firing since May 25 from Afghanistan.
In a similar attack on Monday, two soldiers were killed.
Both countries accuse each other of backing cross-border attacks by militants.