In response to the assault, which also left several Afghan soldiers missing, Afghan president Hamid Karzai postponed a planned trip to Sri Lanka.
General Mohammad Zahir Azimi, a spokesman for the Defence Ministry, said “hundreds” of foreign and Afghan insurgents crossed the border to mount the attack, which took place in the remote and mountainous Ghazi Abad district of Kunar Province in the early hours of yesterday.
Mr Azimi did not specify which border, but Kunar lies next to Pakistan. It is a militant stronghold, and many Arab and other foreign insurgents are believed to operate there alongside the Afghan Taliban.
The Taliban claimed responsibility for the attack in an emailed statement, saying that one of their insurgents was killed and two were wounded.
The group has escalated attacks in recent months as it tries to take advantage of the withdrawal of foreign troops at the end of 2014.
Casualties among Afghan troops have been rising significantly since they took the lead in the war against the Taliban. Since the beginning of 2014, 84 Afghan army soldiers have been killed.
Mr Azimi said that 21 Afghan soldiers died and three others were wounded in the attack, which turned into an intense, four-hour gunbattle between the army and insurgents.
An army support unit en route to assist the operation was also targeted by a suicide bomber, he said, but there were no military casualties.
General Abdul Habib Sayedkhaili, chief of police for Kunar Province, said that there were around 30 Afghan soldiers manning the outpost when insurgents attacked from three sides with rocket-propelled grenades, mortars and light weapons.
He said that of seven soldiers initially reported missing, three had been found alive and security forces continued to search for the others. It was not immediately clear if the soldiers had been kidnapped or had fled during the assault.
Hours after the attack, security forces were continuing to trade fire with the insurgents and chase them through difficult terrain, Mr Saeydkhaili said.
“Our security forces are going as far as they can to chase the enemy,” he added.
While neither Mr Azimi nor Mr Sayedkhaili specifically mentioned Pakistan, the Afghan president appeared to point a finger at the neighbouring nation.
In a statement condemning the attack, Mr Karzai called on Pakistan to take serious measures to destroy terrorist sanctuaries and fight against terrorism.
Pakistan has a complicated relationship with the Taliban. It helped the group seize control of Afghanistan in 1996, and Kabul has repeatedly accused Islamabad of providing the insurgents sanctuary on its territory following the US-led invasion of Afghanistan in 2001.
Meanwhile, a bomb which exploded at a busy bus terminal near a police station in north-west Pakistan killed 14 people and injured 15 others, officials said
The explosion targeted passengers in a motorised rickshaw and those on a minibus in Kohat, near the country’s lawless tribal region, some 100 miles west of Islamabad.
No one immediately claimed responsibility for the attack, though Kohat has seen past attacks by local Taliban fighters and allied sectarian groups against its minority Shiite population, which has a presence in the city.
North-western provincial police chief Nasir Durrani said security forces have been conducting counter-insurgency operations in the area.