The gunmen, dressed in police uniforms, stormed the Badaber air base on Friday morning, Maj Gen Asim Bajwa said.
At least 16 of the victims were killed during morning prayers at a mosque inside the compound, Gen Bajwa added.
The Pakistani Taliban said they ordered the attack. Peshawar has frequently been targeted by militants.
Gen Bajwa said the militants entered the base, which is 10km (6 miles) south of Peshawar, at two points and then split into groups.
A rapid response force was dispatched to the scene and contained the attackers around a guard room, he added.
Thirteen militants were said to have been killed by security forces, although the Taliban said just one of their fighters died.
The total number of gunmen involved is unclear, but Gen Bajwa said his forces were hunting for the remaining attackers.
The exchange of fire also left 29 people injured, the Pakistani military said.
Gen Bajwa claimed that mobile phone intercepts suggested that the attackers had come from inside Afghanistan, though he said there was no reason to blame the Kabul government.
The attack on Badaber air base is the first on a military target since a Pakistan navy ship was attacked in Karachi's dockyard in September last year. It is the bloodiest since last December's massacre of 150 pupils and teachers at Peshawar's Army Public School.
The air base - which is essentially a residential complex rather than an operational one - is located on the southern-most tip of Peshawar's administrative limits. It is surrounded by tribal territory, which has been the hub of criminal and militant activity until recently.
The attack comes amid claims of success by the military in its 15-month operation in the tribal region, and may well be an attempt by militants to show they can still hit hard targets.
It also exposes holes in Pakistan's pre-emptive intelligence gathering mechanisms, mainly due to lack of co-ordination and information sharing among various security agencies.
In an email, Pakistani Taliban spokesman Muhammad Khurasani said a "suicidal unit" had carried out the attack.
An un-named military official told the Reuters news agency that the militants had "explosives-laden jackets and were armed with hand-propelled grenades, mortars, AK-47 rifles".
It is so far unknown if the militants had any insider support, as has been the case in previous attacks.
Pakistan's Prime Minister Nawaz Sharif condemned the attack, saying: "Terrorists will be rooted out from the country."
The military launched operations in North Waziristan in June 2014 to target militant hideouts, involving air force and ground troops.
Correspondents say this has helped lead to a significant reduction in large-scale Taliban attacks in Pakistan.
According to some reports, militant attacks decreased by 70% in 2015.

Sum: Taliban militants have killed at least 29 people in an attack on an air force base in the northern city of Peshawar, Pakistani officials say.


Crowds lined the streets for the Riding of the Marches on Sunday.
The historic event, which dates back to the 16th century, sees riders make a seven-hour journey from the Braid Hills towards the city, ending at the Mercat Cross on the Royal Mile.
Now an annual event, it returned to Edinburgh in 2009 after a gap of more than 60 years.
It sees the appointed Edinburgh Captain and Lass lead hundreds of horses up the Royal Mile where they return the city flag to the Lord Provost.
The Riding of the Marches was first recorded in the capital in 1579 and was held on a regular basis until 1718, where the purpose was to inspect the boundary markers of Edinburgh.
The riding tradition, known as "common riding" in the Borders, is believed to date as far back as the 12th century, when Scottish towns were granted a charter, effectively creating the birth of the town.

Sum: About 280 horses and riders have taken to Edinburgh's streets, continuing an ancient tradition.


Patrick Maloney, 43, described in court as a "gentle giant", died of multiple injuries at Toddbury Farm, near Leighton Buzzard last August.
Christopher McCarthy, 22, from the nearby Greenacres site, was convicted at Luton Crown Court of manslaughter.
McCarthy's bother Thomas was found not guilty of causing actual bodily harm.
More on this and other stories at BBC Local Live: Beds, Herts & Bucks
Prosecutor Ann Evans said that after consulting the Maloney family, Thomas McCarthy would not face a retrial. The judge entered a not guilty verdict on the ABH charge.
During the trial, the jury heard of a "simmering feud" between two traveller families.
The court was told how McCarthy drove to the scene of the killing and saw Mr Maloney, who was 6ft 4in tall, attacking a BMW car with his parents and sister inside.
He drove into Mr Maloney and dragged his body in a circular motion over gravel.
McCarthy claimed he had no intention of striking Mr Maloney.
But Judge Michael Kay QC said: "Instead of swerving to avoid him or to brake you drove straight at him. You did not take any evasive action.
"This was a calculated, albeit split-second, decision to drive at Patrick Maloney because you were angry at him for attacking your parents' vehicles."
In a witness impact statement read, Mr Maloney's wife, Margaret, said: "I loved my husband with all my heart. He had a smile for everyone. He was everyone's friend. People said he was a gentle giant."
His son, also called Patrick, said his whole world fell apart when his father died in his arms. Referring to McCarthy in the statement he said: "You have ripped our lives apart. Nightmares wake me up at night."

Sum: A man who killed a rival in a "simmering" traveller feud by dragging him beneath his van has been jailed for 10 years.


Paul Wilcox, originally from Warwickshire, sustained severe injuries on his right leg at the popular tourist destination in New South Wales.
A swimmer brought him in to the beach but he was pronounced dead.
Beaches in the area have been closed for 24 hours and people have been warned to stay out of the water.
Mr Wilcox, a British information technology specialist, had been living in Australia for more than 10 years.
"At 10:45 police and paramedics were called to Main Beach after reports of a shark attack," New South Wales police said in their initial statement.
"He was in the water when he was bitten.  He was seen floating in shallow water, close to the shore line, and dragged onto the beach. An ambulance was called and he was pronounced dead a short time later," the statement said.
Police said Mr Wilcox's wife was watching from the shore, ABC reported.
Witness Mark Hickey said he tried to help.
"I saw what looked like seaweed but it was blood in the water," he said. "I didn't know it was a person but when I realised, I ran out and waded to the bank and grabbed him and did CPR but it was too late."
He told local media he saw a "six or seven foot" shark in the water.
Rescue organisation Surf Lifesaving Australia said the beach was not supervised at the time of the attack as it had occurred a week before seasonal duties.
"At the council's request, lifeguards will be on duty for at least the next 24 hours ensuring the water remains clear," it said in a statement.
Sources: Tarong.org.au, Australian media reports
There have been several shark attacks off Australia's beaches in recent years.
In April, a woman was killed as she swam off Tathra beach, also in New South Wales.
The Western Australia government, meanwhile, has ordered a cull of sharks, following at least six fatal attacks off the state's beaches in three years.
Baited hooks have been installed off Perth's popular beaches. Any shark more than three metres long - which could include Great White, Tiger and Bull sharks - will be shot.
The controversial move prompted thousands of people to take part in protests against the culls.

Sum:
A 50-year-old British man has been killed in a shark attack off a beach in Byron Bay, the most easterly point on the Australian mainland, officials say.