Egypt's military said militants firing machineguns and rocket-propelled grenades attacked a border checkpoint Saturday, killing 21 soldiers in one of the biggest assaults on security forces in years.
The attack in a desert area 630 kilometres (390 miles) west of Cairo also left four soldiers wounded, the military said in a statement, blaming "terrorists".
It said a rocket propelled grenade fired by the militants set off an explosion in an ammunition depot at the al-Farafrah post, killing the soldiers.
Militants have stepped up attacks on the security forces since Islamist president Mohamed Morsy was toppled in July 2013 as the army struggles to quell an Islamist insurgency that has killed scores of soldiers and police, mainly in the Sinai Peninsula bordering Israel and the Palestinian Gaza Strip.
The military said two vehicles booby trapped to blow up were used in the attack, and bomb experts have defused the explosives.
State news agency MENA said three of the assailants were killed in the assault, the second at the same checkpoint in less than three months.
The attack followed repeated warnings by officials of a possible spillover of violence from across the border with Libya, where relentless bloodshed over the past few months has sparked fears of all-out civil war.
Libya has been awash with weapons and gripped by unrest since the NATO-backed uprising that toppled dictator Muammar Qadhafi in 2011, with rival militias who ousted him vying for control.
Egypt's porous western border with Libya stretches for more than 1,000 kilometres (620 miles).
Rocket wounds soldier
The presidency announced three days of national mourning after the attack, one of the biggest since more than a dozen soldiers were killed by gunmen in August 2012 in the town of Rafah that borders with Israel and Gaza
That assault, like Saturday's, occurred during the month of Ramadan when soldiers were eating the traditional iftar meal to break their fast.
Saturday's attack also comes just days after seven civilians and a soldier were killed in a series of rocket attacks in the restive Sinai.
A similar attack on Egyptian border guards in June killed six guards.
Most of the assaults that have surged since Morsy's ouster have been claimed by jihadists battling a bloody crackdown by the authorities on his supporters.
Aside from the Sinai, other areas including Cairo have been targeted, while bombings and assassinations have killed top police officers.
Since the army removed Morsy, a police crackdown on his supporters has left more than 1,400 people dead in street clashes, upwards of 15,000 jailed and some 200 sentenced to death.
Morsy himself and several senior leaders of his Muslim Brotherhood movement have been put on trial.
Egyptian officials accuse the Brotherhood of being behind attacks and designated it a "terrorist group" in December after a deadly car bombing on a police station north of Cairo killed more than a dozen people.
The Islamist movement has denied involvement and many of the attacks have been claimed by Sinai-based jihadist group Beit al-Maqdis (Partisans of Jerusalem).
Officials say groups such as Beit al-Maqdis are linked to the Brotherhood, and often accuse the Islamist movement of funding the jihadists.
On 10 July, Egypt's security forces seized 20 Grad rockets being smuggled from the Gaza Strip through a tunnel by militants in northern Sinai.
They were seized two days after the latest conflict erupted between Israel and militants from Palestinian Hamas movement, an affiliate of Egypt's Muslim Brotherhood.
On Thursday, Israel launched a ground operation in Gaza aimed at destroying the network of sophisticated cross-border tunnels.
A rocket probably fired from the Gaza Strip hit Egypt's Rafah border crossing on Saturday, wounding a soldier, a security official said.
Egypt usually keeps the crossing closed but has allowed in Palestinians from Gaza wounded in the ongoing conflict.