Egypt's Islamist President Mohamed Mursi dismissed Cairo's two top generals and quashed a military order that had curbed the new leader's powers, in a move that further stamped his authority on the country and its army.
There had been much debate over the fate of Field Marshal Hussein Tantawi, 76, who until Mursi's election in June had ruled Egypt as head of a military council since Hosni Mubarak was toppled last year. The timing of Sunday's announcement to replace him as armed forces head was nevertheless a surprise.
However, an embarrassing debacle for the army on the border with Israel, where 16 Egyptian troops were killed by Islamist militants a week ago, may have given Mursi the opening he needed to step up the pace in rolling back the military's influence, pushing aside Tantawi and military chief of staff Sami Enan.
Mursi's spokesman called it a "sovereign" decision by the head of state, and aimed at "pumping new blood" into an army that has shown signs of hoping to control the novice president. A fellow Islamist said Egypt could not go on having "two heads".
Secular activists, wary of political Islam, nonetheless welcomed a "first step toward establishing a civilian state".
Mursi himself later said: "The decisions I took today were not meant ever to target certain persons, nor did I intend to embarrass institutions, nor was my aim to narrow freedoms.
"I did not mean to send a negative message about anyone, but my aim was the benefit of this nation and its people," he said, praising the work of the armed forces and saying his decision would free them to focus on their professional tasks.
The move sidelines Tantawi, Mubarak's defence minister for two decades and whose continued presence had cast a shadow of military rule over the new democracy, and whittles away powers still held by the army, from whose ranks all Egyptian presidents for the past 60 years had been drawn until the voting in June.
A member of the military council told Reuters that Mursi, a moderate Islamist party official popularly elected in June but with constitutional powers sharply circumscribed in advance by the generals, had consulted Tantawi and General Enan, 64, before ordering both men to retire.
But it was not clear how far the generals, members of the Supreme Council of the Armed Forces (SCAF), actually consented to a move that reveals a reordering of Egypt's political forces as they all wait for a new constitution, shifting more powers towards Mursi and his long-suppressed Muslim Brotherhood.
"This clash between the new president and the military council was expected - but not this fast," said analyst Gamal Soltan. "It can be considered a restructuring of the armed forces and an end to the role of SCAF in political life."
Thousands of Islamist supporters gathered in Cairo's Tahrir Square and other cities to back Mursi's decision. "President of the republic, your decree gets 100 percent," some chanted.
Tantawi, after serving Mubarak as a minister for 20 years, helped ease the ageing dictator out of office on February 11, 2011 in the face of the mass street protests of the Arab Spring.
A senior Brotherhood official, Mahmoud Ghozlan, said the army should have returned to barracks once Mursi was elected. But it had instead sought to retain a role in politics: "The nation came to have two heads - the president and the military council," Ghozlan said. "The president had to act to recover his full powers from the hands of the military council."
In a statement that came out of the blue, presidential spokesman Yasser Ali announced: "Field Marshal Hussein Tantawi has been transferred into retirement from today." In his place as armed forces chief and defence minister, Mursi appointed General Abdel Fattah al-Sisi, 57, from military intelligence.
Enan was replaced General Sidki Sobhi, 56, who headed the Third Field Army based in Suez, on the border with Sinai.
Both those pushed into retirement, whose positions may have been weakened by the border debacle last week in the Sinai desert, were appointed as advisers to the president.
By scrapping the army's constitutional declaration, Mursi can also take on the legislative powers the generals had sought to keep for themselves in the absence of a parliament. In June, the military council, backed by judges, dissolved the Islamist-led assembly elected in January - a move Mursi has challenged.
"The decision was a sovereign one, taken by the president to pump new blood into the military establishment in the interests of developing a new, modern state," presidential spokesman Ali told Reuters after making the announcement.
"It was a critical decision and the members of the military council understood this because they are patriotic and General Sisi is from the new generation of the patriotic men of the armed forces. He is responsible and well respected."
Enan was long seen as particularly close to the Pentagon, the main sponsor of Egypt's armed forces. Washington gives Egypt $1.3 billion in military aid each year.
Liberals and other political rivals of the Brotherhood have voiced concerns at the growing might of the Islamists, who for decades were hounded and jailed by Mubarak and his predecessors. But they have also been wary of the army's continuing role.
The April 6 youth movement, which helped galvanize the revolt against Mubarak, described Mursi's move as the "first step towards establishing a civilian state", it said on its Facebook page. "We want a strong, national army to protect the nation and that does not intervene in politics".
Tantawi's age had meant that his departure had been long expected in some form, and his appointment as an adviser to Mursi appeared to exclude the possibility he might face the kind of prosecution that saw Mubarak, now 84, jailed for life.
In a similar vein, two other senior officers in the navy and air defence retired into high office at the Suez Canal Authority and an industrial body, mirroring the kind of lucrative sinecure posts offered to officers in Mubarak's era.
The armed forces, with vast resources in Egypt's economy as well as a military strength funded in part by Washington, remain a key institution in Egypt and the process of establishing full democratic control has only just begun.
General Mohamed el-Assar, who sits on the military council, told Reuters: "The decision was based on consultation with the field marshal, and the rest of the military council."
Mursi, whose election victory over a former general prompted concerns in Israel and the West about alliances with Egypt, also appointed a judge, Mahmoud Mekky, as his vice president. Mekky is a brother of newly appointed Justice Minister Ahmed Mekky, who had been a vocal critic of vote-rigging under Mubarak.
Mekky and Sisi were shortly afterwards shown on state television with Mursi, being sworn in to their new positions.
Mursi, who has pledged to uphold democratic accountability and to stand by Cairo's treaties with Israel and other states, has shown impatience with the military following violence in the Sinai desert that brought trouble with Israel and the Palestinians' Gaza Strip enclave this month.
The president, whose own Brotherhood movement renounced violence to achieve political change in Egypt long ago, sacked Egypt's intelligence chief last week after the attack in which Islamist militants killed the 16 Egyptian border guards before trying to storm the Israeli border.
"The timing of the sacking of Tantawi will serve President Mursi as many Egyptians had blamed the military for the events (on the border) and would not stand against this decision," said Mustapha Al Sayyid.
One analyst said Mursi may also have acted to ensure the military commanders did not recoup their lost credit after a military operation launched in Sinai to hunt down the militants, the biggest such operation since Egypt's 1973 war with Israel.
On Sunday, officials said Egyptian troops had killed five Islamist militants after storming their hideout near the isolated border with Israel.
(Additional reporting by Yasmine Saleh, Marwa Awad, Shaimaa Fayed and Ali Abdelatti; Writing by Edmund Blair; Editing by Alastair Macdonald)