Pakistan began a military offensive against Islamic State-affiliated fighters along the northern part of the border last month, saying the region was fast becoming a safe haven for multiple terrorist organizations.
An army patrol was conducting a search operation in Upper Dir on Tuesday night, near the Malakand region where the Pakistani Taliban had controlled large swaths of territory until a military operation in 2009.
"The military officials received information about the presence of terrorists in the area," the military said in a statement. They were conducting a search operation "when one of the terrorists blew himself up".
An officer and three soldiers were killed in the attack, which drew immediate condemnation from new Prime Minister Shahid Khaqan Abbasi.
"Each drop of blood of our heroes is a testament to the fact that we all are united as a nation to cleanse our beloved motherland from terrorist elements," the prime minister's office said in a statement.
Abbasi took office after previous prime minister Nawaz Sharif was disqualified by the Supreme Court over undeclared assets, a ruling Sharif has decried as politically motivated but opposition leaders say is a victory for the rule of law.
A security official who asked not to be identified told Reuters that security forces were drawn into a gun battle with two militants when one of the men blew himself up.
"This area is infamous for its record of terrorist activities," the official said.