CAIRO — Egypt's former president, Mohammed Morsi, the Muslim Brotherhood leader who rose to office in the country's first free elections in 2012 and was ousted a year later by the military, collapsed in court during a trial and died Monday, 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.

The Brotherhood accused the government of "assassinating" Morsi through years of poor prison conditions during which he was often kept in solitary and barred from visits. Egypt's chief prosecutor said a team of forensic experts would examine Morsi's body 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" — the pro-democracy uprising that in 2011 ousted the country's longtime authoritarian leader, Hosni Mubarak.

Morsi's Muslim Brotherhood, Egypt's most powerful Islamist group, dominated 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.

Critics accused the Brotherhood of seeking to monopolize power and using violence against opponents, and soon massive protests grew against their rule. In July 2013, the military — led by then-Defense Minister, Abdel-Fattah el-Sissi — ousted Morsi, 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 was 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, while the military has come to dominate politics behind the scenes.

Since his ouster, Morsi and other Brotherhood leaders have been in prison, undergoing multiple 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, being 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 poor conditions fall far below Egyptian and international standards. Morsi was known to suffer from diabetes.

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."

Morsi's son, Ahmed, confirmed the death of his father in a Facebook post, saying, "We will meet again with God."

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."

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."

"They placed him in solidarity confinement throughout his detention which exceeded 5 years, prevented medicine and provided poor food," it said. "They prevented doctors and lawyers and even communicating with his family. They deprived him from the simplest human rights."

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 before collapsing inside the cage. "He was very calm and organized. He summarized our argument in three to five minutes. He insisted on a special tribunal as he is the president of the republic," Madour said.

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.

When elected, he 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 using elections to entrench their own control.

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

Still, the Brotherhood never managed to control all levers of power, facing opposition within courts and in the powerful security forces. As protests grew, the military stepped in: Army commandos took him and other Brotherhood leaders into custody.

The subsequent crackdown has all but completely dismantled the Brotherhood, with hundreds killed and thousands imprisoned, with most other active figures fleeing abroad. 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."