The army chief came to President Mohammed Morsi with a simple demand: Step down on your own and don't resist a military ultimatum or the demands of the giant crowds in the streets of Egypt.

"Over my dead body!" Morsi replied to General Abdel-Fattah el-Sissi, two days before the army eventually ousted the Islamist leader after a year in office.

In the end, Egypt's first freely elected president found himself isolated, with allies abandoning him and no one in the army or police willing to support him.

Even his Republican Guards simply stepped away as army commandos came to take him to an undisclosed Defence Ministry facility, according to army, security and Muslim Brotherhood officials who gave The Associated Press an account of Morsi's final hours in office. They spoke on condition of anonymity because they were not authorised to talk to the media.

The Muslim Brotherhood officials said they saw the end coming for Morsi as early as June 23 - a week before the opposition planned its first big protest. The military gave the president seven days to work out his differences with the opposition.

In recent months, Morsi had been at odds with virtually every institution in the country, including the top Muslim and Christian clerics, the judiciary, the armed forces, the police and intelligence agencies.

His political opponents fuelled popular anger that Morsi was giving too much power to the Brotherhood and other Islamists, and had failed to tackle Egypt's mounting economic problems.

There was such distrust between Morsi and the security agencies that they began withholding information from him - deploying troops and armour in cities in the past week without his knowledge, the officials said.

The police also refused to protect Muslim Brotherhood offices that came under attack in the latest wave of protests.

Thus, when Morsi was fighting for his survival, there was no one to turn to, except calling for outside help through Western ambassadors and a small coterie of aides from the Brotherhood who could do little more than help him record two last-minute speeches.

In those remarks, he emotionally emphasised his electoral legitimacy - a topic that Morsi repeatedly raised in the talks with el-Sissi.

Early this week, during two meetings in as many days, Morsi, el-Sissi and Hesham Kandil, the Islamist-backed prime minister, sat down to discuss ways out of the crisis in which millions of Egyptians were clamouring for the president to resign.

But Morsi kept returning to the mandate he won in the June 2012 balloting, according to one of the officials. He said Morsi wouldn't address the mass protests or any of the country's most pressing problems - tenuous security, rising prices, unemployment, power cuts and traffic congestion.

A Brotherhood spokesman, Murad Ali, said the military had already decided that Morsi had to go, and el-Sissi would not entertain any of the concessions that the president was prepared to make.

"It was like, 'Either we put you in jail, or you come out and announce you are resigning,'" Ali added. "He didn't do either because he didn't want to hand the country to the military again."

But according to one official, Morsi delivered the final, terse response to el-Sissi's demand: "Over my dead body!"

On Monday, the armed forces announced they had given Morsi 48 hours to meet the protesters' demands or face military intervention.

In reality, however, the countdown had begun as early as June 23, when el-Sissi gave Morsi and the opposition a week to work out their differences - a remote possibility given the wide gap between both sides.

Brotherhood officials said they saw the end coming based on el-Sissi's comments nine full days before Morsi's actual ouster.

"We knew it was over on June 23. Western ambassadors told us that," said another Brotherhood spokesman. US Ambassador Anne Patterson was one of the envoys, he added.

Morsi searched for allies in the army, ordering two top aides - Asaad el-Sheikh and Rifaah el-Tahtawy - to establish contact with potentially sympathetic officers in the 2nd Field Army based in Port Said and Ismailia on the Suez Canal.

The objective was to find army allies to use as a bargaining chip with el-Sissi, security officials with firsthand knowledge of the contacts said.

There were no signs that Morsi's overtures had any effect, but el-Sissi, on learning of the contacts, took no chances. He issued directives to all unit commanders not to engage in any contacts with the presidential palace and, as a precaution, dispatched elite troops to units whose commanders had been contacted by Morsi's aides.

The commander of the 2nd Field Army, Lieutenant General Ahmed Wasfy, denied that there were any divisions within the military.

"We are united. The culture and principles of the armed forces don't allow divisions," he said.

On the surface, Morsi wanted to give the impression that the government was conducting business as usual.

His offices released statements about meetings with Cabinet ministers to discuss issues such as the availability of basic food items during the holy month of Ramadan when Muslims feast on food after a day of dawn-to-dusk fasting. He had four Cabinet ministers talk to TV reporters in the presidential palace about fuel shortages and power cuts.

The opposition set its first mass protest calling for his ouster on Sunday, June 30, the anniversary of his inauguration, but the demonstrations began early, and Morsi had to stop working at Ittihadiya palace on June 26.

The next day, he and his family moved into the Cairo headquarters of the Republican Guards, an army branch that protects the president, his family and his palaces and residences.

Morsi worked at the Qasr El Qouba palace and continued to do so until June 30, when the Republican Guards advised him to stay put at their headquarters.

His foreign policy aide, Essam el-Haddad, telephoned Western governments about what was happening in Egypt, and putting an optimistic spin on events, according to a military official.

El-Haddad was also issuing statements in English to the foreign media, saying that the millions out on the streets did not represent all Egyptians, and that the military intervention amounted to a textbook coup.

According to the usually authoritative newspaper Al-Ahram, Morsi was offered safe passage to Turkey, Libya or elsewhere, but he declined. He also was offered immunity from prosecution if he voluntarily stepped down, but he again said no, the newspaper added.

Morsi gave a speech late on Tuesday night in which he vowed to stay in power and urged supporters to fight to protect his legitimacy.

Soon after he finished, el-Sissi placed him under "confinement" in the Republican Guards headquarters. Some of his close aides, including el-Haddad, stayed with him.

Troops began deploying across major cities at 5am Wednesday, ahead of the expiration of the two-day ultimatum given by the army.

On the army's official Facebook page, videos of the deployment were posted to reassure Egyptians.

At noon Wednesday, hours before el-Sissi announced the ouster, the Republican Guards' troops assigned to protect Morsi walked away from the president and his aides.

Army commandos soon arrived. There was no commotion, and Morsi went quietly, taken to an undisclosed Defence Ministry facility, officials said.