The army chief came to President Mohammed Morsi with a simple demand: Step down on your own.

"Over my dead body!" Morsi replied to General Abdel Fatah al-Sisi on Monday, two days before the army eventually ousted him after a year in office.

In the end, Egypt's first freely elected president found himself isolated, abandoned by allies 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.

The Muslim Brotherhood officials said they saw the end coming for Morsi as early as 23 June – 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 leading 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 Muslim 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 without his knowledge.

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

Therefore, 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 Sisi.

Early this week, during two meetings in as many days, Morsi, Sisi and Hesham Kandil, the prime minister, sat down to discuss ways out of the crisis.

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 Sisi would not entertain any of the concessions that the president was prepared to make.

"We were naive ... We didn't imagine betrayal would go this far," Ali said.

"It was like, 'either we put you in jail, or you come out and announce you are resigning,'" Ali added.

Brotherhood officials said they saw the end coming.

"We knew it was over on 23 June. 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 a bargaining chip to use with Sisi, security officials with firsthand knowledge of the contacts said.

There were no signs that Morsi's overtures had any effect, but Sisi, 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 end nears

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 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 had set its first mass protest for 30 June, the anniversary of his inauguration, but the demonstrations began early, and Morsi had to stop working at Ittihadiya palace on 26 June.

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

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

His foreign policy aide, Essam el-Haddad, telephoned western governments to put an optimistic spin on events, according to a military official. 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.

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

Soon after, Sisi placed him under "confinement" in the Republican Guard headquarters. The next day the military's deadline to Morsi expired. At 5am troops began deploying across major cities and the military posted videos of the movements to its Facebook page in a bid to reassure the public. Republican Guards assigned to the president and his aides walked away at midday and army commandos arrived.

There was no commotion and Morsi went quietly. That evening, Sisi announced Morsi's removal.