Egyptians shout slogans against President Abdel Fattah al-Sisi in Cairo in April, in protest of a land deal to transfer control of two islands in the Red Sea to Saudi Arabia. On Tuesday, an Egyptian court rejected the deal, an unusual step and a potential setback for Mr. Sisi given the judiciary's traditional backing of government actions.

In a surprising setback to President Abdel Fattah al-Sisi, an Egyptian court nullified an agreement on Tuesday that would transfer control of two islands in the Red Sea to Saudi Arabia.

The conflict over the dry, uninhabited Tiran and Sanafir islands had gripped Egypt for months, since Mr. Sisi announced the transfer during a visit by Saudi monarch King Salman in April.

The Egyptian president portrayed the transfer of the islands as a return to Saudi Arabia for the first time since 1950, when the Saudis placed them under Egyptian control following fears that Israel could seize them, The New York Times reports. The land transfer came amid a variety of economic agreements Sisi signed with the Saudi government, including a development deal in the Sinai Peninsula.

But protesters rooted the conflict in a deeper sense that the islands were Egyptian, while critiquing Sisi's leadership in the years since he led the military ouster of Muslim Brotherhood leader Mohammed Morsi, Egypt's first democratically elected president, in 2013.

"By nature the Egyptian people are attached to their land, and historically most Egyptians worked in agriculture," political activist Ahmed Abdullah told Al-Monitor in April. "Land for Egyptians is a matter of honor."

"Awad sold his land," crowds in Cairo have shouted, likening Sisi to the protagonist of a folktale about a man who brought shame to his family by giving up his family farm. The protests prompted a police crackdown, Al-Monitor reports.

On Tuesday, Judge Yehia al-Dakroury's ruling that Egyptian sovereignty over the islands, which are located at the mouth of the Gulf of Aqaba, couldn't be amended in favor of another state, came as somewhat of a surprise.

The ruling must still be approved by Parliament, and government officials said they would appeal the judge's decision, Reuters reports.

In the past, the Egyptian judiciary has often been thought of as deferring to the country's leaders, jailing hundreds of government critics.

While Sisi is overseeing a crackdown on the Muslim Brotherhood, his government has also increased prosecutions of people accused of blasphemy.

Now, the judge's ruling against the land deal with Saudi Arabia could potentially set some senior Egyptian officials up for prosecution themselves.

Under Egyptian law, officials who negotiate a deal with foreign government that harms national interests can face a life sentence, though legal experts are divided on whether this could be the case with the land deal.

The lawyers who filed the case called the decision a victory. The judge's decision demonstrates the courts "are fair and only care about the interests of the country," Essam el-Eslamboly, one of the Egyptian lawyers who challenged the transfer, told The New York Times.

More than 150 people have been given jail sentences or fines in connection with protesting the land deal, Reuters reports, while lawyers are pushing for their release.