The fighting took place as Pakistani troops, backed by helicopter gunships, carried out a search operation around the town of Bara in the lawless Khyber tribal district bordering Afghanistan.
"Four paramilitary soldiers were martyred and seven others were wounded in today's fighting," a senior security official told AFP.
"At least 39 militants were also killed," he said.
Four civilians also died when a mortar shell hit a house, a government official said.
"Four civilians were killed in the day-long gunfight when a mortar shell hit their house," Khyber's deputy administration chief Syed Ahmed Jan told AFP.
It was not immediately clear who fired the mortar shell, he said.
A senior paramilitary official said the militants belonged to the Taliban-linked Laskhar-e-Islam group that is led by warlord Mangal Bagh.
Independent verification of the incident is not possible as access to the area is restricted by the military.
Some 18,000 people fled their homes in Khyber in October last year amid fears of a fresh outbreak of fighting between the army and Islamist militants linked to the Pakistani Taliban.
Pakistan's seven tribal districts near the Afghan border are rife with homegrown insurgents and are strongholds of Taliban and Al-Qaeda operatives.
Islamist militants have killed more than 4,800 people across Pakistan since government troops raided an extremist mosque in Islamabad in July 2007.