A statement from the military's public relations wing said the exchange of fire in North Waziristan took place after the army personnel had surrounded the militant hideout early Sunday.
"As soon as the troops cordoned off the area, terrorists opened fire. All dug out terrorists were shot down by security forces, " the statement said. "In exchange of fire, four soldiers embraced shahadat (martyrdom)."
The militants were not identified, but Pakistan's military has been battling members of the Pakistani Taliban group, known as the Tehreek-e-Taliban (TTP) in North Waziristan for years.
TTP, which has also voiced support for the Islamic State in Afghanistan, declared war on the government of Pakistan more than a decade ago. Since then, they have carried out a series of attacks, including a brutal assault on a Pakistan Army Public School in northwest Peshawar in December 2014, killing 140 children and several teachers.
Pakistan's militant groups are often interlinked with those across the border in Afghanistan, so its progress at reining in terror is critical, particularly as Washington seeks to forge ahead on an agreement with the Afghan Taliban to bring an end to the 18-year war.
The Associated Press contributed to this report.