Mr. Morsi watched from inside a metal cage that is used in Egyptian courts to hold defendants.

The trial has been held in the same auditorium at the Police Academy in Cairo where Mr. Mubarak was tried while Mr. Morsi was president. Mr. Morsi was held in the cage with dozens of other defendants, mostly from the Muslim Brotherhood. After the first session of Mr. Morsi’s trial, thick soundproof glass was installed around the cage to prevent the defendants from interrupting the proceedings by shouting or chanting.

The prison-break charges stemmed from the events of Jan. 28, 2011, in the last days of Mr. Mubarak’s rule, when the Egyptian police force collapsed in the face of huge demonstrations and the doors of the country’s jails were thrown open. Mr. Morsi, who at the time was a senior leader of the Muslim Brotherhood and the former chief of its parliamentary bloc, seemed to anticipate that he would be questioned about whether he was breaking the law by walking out.

As soon as he was free, he called Al Jazeera to say that he was not fleeing justice. “If there is an official in Egypt that wants to get in contact with us, we’re here, I’m here, the telephone is here,” he said, in comments broadcast by the television network. “We will never run away.”

Still, after he was overthrown, prosecutors charged that Mr. Morsi and others had escaped through an elaborate conspiracy involving both Hamas, the Sunni Muslim militant group in Gaza, and Hezbollah, the Shiite Muslim militant group in Lebanon. The prosecutors claimed that the two groups had sent hundreds of fighters to assault Egypt’s prisons.

Four other senior Brotherhood leaders who were jailed with Mr. Morsi also were sentenced to death in that case. One is Mohamed Badie, the Brotherhood’s top spiritual leader. Mr. Badie and Mr. Morsi still face other pending criminal cases and possible death sentences.

In the espionage case, Mr. Morsi was sentenced on Tuesday to life in prison, which in Egypt is understood to mean 25 years. He was accused of conspiring against Egypt while he was in office as its first democratically elected president. The court did not disclose what evidence it found to support the allegations.