In response to the assault, which also left several Afghan soldiers missing, President Hamid Karzai postponed a planned trip to Sri Lanka.
General Mohammad Zahir Azimi, who is spokesman for the Defense 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 morning hours.
Azimi did not specify which border, but Kunar lies next to Pakistan. It's a militant stronghold, and many Arab and other foreign insurgents are believed to operate there alongside the Afghan Taliban.
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.
More From This Section
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.
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.
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 searching 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, Saeydkhaili said.
While neither Azimi nor Sayedkhaili specifically mentioned Pakistan, the Afghan president appeared to point a finger at the neighbouring nation.