CAIRO — Egypt’s first democratically elected president, Islamist leader Mohammed Morsi, who was ousted by the military in 2013 after a year in office, collapsed in court while on trial Monday and died, state TV and his family said.

The 67-year-old Morsi had just addressed the court, speaking from the glass cage he is kept in during sessions and warning that he had “many secrets” he could reveal, a judicial official said. A few minutes afterward, he collapsed in the cage, the official said, speaking on condition of anonymity because he was not authorized to talk to the press.

In his final comments, he continued to insist he was Egypt’s legitimate president, demanding a special tribunal, one of his defense lawyers, Kamel Madour told the Associated Press. State TV said Morsi died before he could be taken to the hospital.

Morsi’s Muslim Brotherhood accused the government of “assassinating” him through years of poor prison conditions. Morsi, who was known to have diabetes, had been imprisoned since his 2013 ouster, often in solitary confinement and barred from visitors — his family was allowed to visit only three times during that time. Egypt’s chief prosecutor said Morsi’s body would be examined to determine the cause of his death.

It was a dramatic end for a figure who was central in the twists and turns taken by Egypt since its “revolution” — from the pro-democracy uprising that in 2011 ousted the country’s longtime authoritarian leader, Hosni Mubarak, through controversial Islamist rule and now back to a tight grip under the domination of military men.

The Muslim Brotherhood, Egypt’s most powerful Islamist group, won the elections held after Mubarak’s fall, considered the first free votes the country had seen. First, they gained a majority in parliament, then Morsi squeaked to victory in presidential elections held in 2012, becoming the first civilian to hold the office.

Critics accused the Brotherhood of using violence against opponents and seeking to monopolize power and Islamize the state. Massive protests grew against their rule, until the military — led by then-Defense Minister, Abdel-Fattah el-Sissi — ousted Morsi in July 2013, dissolved parliament and eventually banned the Brotherhood as a “terrorist group.”

El-Sissi was elected president and re-elected in 2018 in votes human rights groups sharply criticized as undemocratic. He has waged a ferocious crackdown that crushed the Brotherhood but also almost all other dissent, arresting tens of thousands, banning protests and silencing most criticism in the media.

Since his ouster, Morsi and other Brotherhood leaders have been put on multiple and lengthy trials. Morsi was sentenced to 20 years in prison on charges of ordering Brotherhood members to break up a protest against him, resulting in deaths. Multiple cases are still pending. Monday’s session was part of a retrial, held inside Cairo’s Tora Prison, on charges of espionage with the Palestinian Hamas militant group.

Morsi was held in a special wing in Tora nicknamed Scorpion Prison. Rights groups say its conditions fall far below Egyptian and international standards.

In contrast, Mubarak was allowed to stay in a military hospital during trials on various charges — of which he was eventually cleared and allowed free.

Sarah Leah Whitson, Middle East director with the Human Rights Watch, said in a tweet that Morsi’s death was “terrible but entirely predictable” given the government “failure to allow him adequate medical care, much less family visits.”

Mohammed Sudan, leading member of the Muslim Brotherhood in London, said Morsi was banned from receiving medicine or visits and there was little information about his health condition.

”This is premeditated murder. This is slow death,” he said.

Freedom and Justice, the Brotherhood’s political arm, said in a statement on its Facebook page that prison conditions led to Morsi’s death in what amounted to “assassination.”

The judicial official said Morsi had asked to speak to the court during Monday’s session. The judge permitted it, and Morsi gave a speech saying he had “many secrets” that, if he told them, he would be released, but he added that he wasn’t telling them because it would harm Egypt’s national security.

Madour, the defense lawyer, said Morsi spoke for around five minutes, “very calm and organized,” before collapsing inside the cage.

A spokesman for the Interior Ministry did not answer calls seeking comment.

Morsi, an engineer who studied at the University of Southern California, was an unlikely figure to be thrust into Egypt’s central stage. He was never considered a major thinker in the Brotherhood and instead rose through its ranks as an efficient, if lackluster, loyalist. The group only put him forward as its presidential candidate in 2012 after a more prominent and powerful figure, Khairat al-Shater, was declared ineligible to run.

The election victories were the crowning point for the Brotherhood, which had been banned under Mubarak but even underground had been the most organized opposition force. Initially, Morsi made gestures toward the secular pro-democracy activists who led the 2011 Arab Spring uprising. But over the course of the year, opponents accused his Brotherhood of hijacking the revolution and trying to entrench Islamist rule.

Major protests erupted, particularly over the process of writing a new constitution in which critics said the Brotherhood was allowing Islamists to write a charter largely on their terms. Brotherhood supporters cracked down violently on some protests.

As protests grew, the military stepped in. Critics called the move a coup, but el-Sissi’s supporters call it a popularly backed move.

The subsequent crackdown has all but completely dismantled the Brotherhood, with hundreds killed and thousands imprisoned, with most other active figures fleeing abroad. At the same time, secular pro-democracy activists were also crushed.

Throughout his trials, Morsi insisted he remained Egypt’s legitimate president. In early court sessions he gave angry speeches until judges ordered him kept in a glass cage during sessions where they could turn off his audio.

In audio leaked from a 2017 session of one of his trials, Morsi complained that he was “completely isolated” from the court, unable to see or hear his defense team, his eyes pained by lighting inside the cage.

”I don’t know where I am,” he is heard saying in the audio. “It’s steel behind steel and glass behind glass. The reflection of my image makes me dizzy.”