Two Pakistani soldiers were killed in cross-border fire from Afghanistan while working on a fence intended to cover nearly all of the disputed 2,500km border separating the countries, the Pakistani military said on Sunday.
Though security has improved in Pakistan's remote areas bordering Afghanistan, sectarian militant attacks, primarily targeting Shias, still occur.
"Pakistani troops are exercising maximum restraint so as to avoid any Afghan civilian casualties," the Pakistan Army's public relations department said, adding that five soldiers were wounded in the attack.
Afghan officials said that Pakistani troops crossed on to Afghan soil, prompting a response by border forces and the local tribal force.
"Two tribesmen were killed and the exchange of fire is ongoing," said Mohmud Zazai, a senior army commander in the area.
Pakistan has blamed Pakistani Taliban militants it says are based on Afghan soil for attacks that have taken place in the region over the past two years, urging Kabul to eradicate "sanctuaries" for militants.
Afghanistan, in turn, accuses Islamabad of sheltering the leadership of the Afghan Taliban militants who are battling the Western-backed government in Kabul.
A local political official, who asked not to be identified, said that Pakistani security forces had responded on Sunday by opening fire along the border.
Mosques in Pakistan's Kurram Agency region, where the attack took place, made announcements urging locals to assist the armed forces, prompting throngs of armed civilians to arrive at the border after the attack.
Kurram has been plagued by militancy over the past decade and was the location of many US drone strikes targeting commanders from al Qaeda and other militant groups.
Twin blasts in Kurram's most populous town, Parachinar, killed more than 75 people in June.
In a separate incident on Sunday in Pakistan's North Waziristan district, which also borders Afghanistan, a security forces vehicle was struck by an improvised explosive device 15km from district capital Miranshah, a military official said.
A curfew has been imposed in the area, with security officials carrying out a search operation.
North Waziristan was a Taliban stronghold until 2014, when Pakistan's military launched a major offensive against the group and pushed many of its fighters across the border into Afghanistan.