The attack, the second this month, occurred in South Waziristan, a lawless tribal region where al-Qaida- and Taliban-linked militants operate.
The militants targeted the Lahda Fort, which houses paramilitary troops, and a nearby observation post in a pre-dawn raid in South Waziristan, located along the border with Afghanistan, the military said in a statement.
Thirty-seven militants and five troops were killed in the intense fighting, the statement said. Two other soldiers were killed and seven wounded in a clash in the neighboring North Waziristan, it said.
The violence in the border region, as well as a series of suicide attacks that have killed hundreds in recent months, is triggering uncertainty in the country ahead of Feb. 18 elections that many predict will weaken President Pervez Musharraf's grip on power.
More than 150 rebels and soldiers are reported to have been killed in the region this month alone.
On Jan. 10, insurgents also attacked Lahda Fort. The military said then that between 40 and 50 of the attackers were killed. Last week, the militants overran a second fort in the region, leaving up to 22 soldiers dead or missing in a major embarrassment for the military.
The latest attack on Lahda Fort came hours before Adm. William Fallon -- the head of the U.S. Central Command and top commander of American forces in the Middle East -- arrived in Pakistan for talks with army chief Ashfaq Kayani, the Pakistan army and the U.S. embassy said.
The army said in a statement the two men discussed the "security situation" in the region, but gave no more details.
The border region emerged as a front line in the war on terror after Pakistan allied itself with the U.S. following the Sept. 11, 2001, terrorist attacks. Washington has given Pakistan billions of dollars in aid to help government forces battle militants.
The region, which has never been fully under the control of the central government, is believed to be home to Baitullah Mehsud, a militant leader blamed by Pakistan and the CIA for masterminding the Dec. 27 assassination of opposition leader Benazir Bhutto.
A purported spokesman for Mehsud warned the military to stop its attacks in the region.
"The army is killing innocent people in our areas and we will take strong action by attacking soldiers wherever possible, if it does not stop such activities," Maulvi Mohammed Umar told The Associated Press by phone.
Umar said one Islamic fighter died and two others were wounded in the attack. He said the militants captured 13 troops in the action, a claim denied by the military.
After the attack on the fort, two fighter jets bombed mountainside villages nearby, killing one civilian, said Rehmanullah Khan, who was injured in the leg by the bombing. Military spokesman Maj. Gen. Athar Abbas said jets had flown reconnaissance missions, but no bombs were dropped.
Meanwhile, security agencies arrested a suspected militant in the southern city of Karachi in connection with the attack on Bhutto, an intelligence official told the AP.
Yousuf Mehsud, who the official said was a close aide to Baitullah Mehsud, was detained late Monday, said the official, who requested anonymity because of the sensitivity of the issue.
Last week, authorities arrested a 15-year-old boy they said was part of the team sent to kill Bhutto.