CAIRO — An Egyptian court on Tuesday nullified a government decision to transfer sovereignty of two strategic Red Sea islands to Saudi Arabia, in a surprising setback for President Abdel Fattah el-Sisi.

The ruling surprised even some of Mr. Sisi’s critics, who did not expect the judiciary to go against the government.

In April, Mr. Sisi transferred Tiran and Sanafir — arid and uninhabited islands at the mouth of the Gulf of Aqaba — to Saudi Arabia during a visit by the Saudi monarch, King Salman.

Saudi Arabia had placed the islands under Egyptian control in 1950, amid fears that Israel might seize them, and Mr. Sisi portrayed their return as a correction of a historical quirk rather than as a sale. Saudi Arabia has been a major financial supporter of Mr. Sisi’s since, as military chief, he led the ouster of his Islamist predecessor, Mohamed Morsi, in 2013.

The islands are of strategic value because they lie at the mouth of the Gulf of Aqaba, the only sea route to the Jordanian port of Aqaba and the port city of Eilat, in Israel.

Egyptian and Saudi officials signed at least 15 agreements during the king’s visit, including a Sinai development package and an oil deal worth $22 billion to Egypt over five years, the state news media reported. But the announcement set off unexpected protests from Egyptians who for decades had considered the islands Egyptian territory, and some critics said that Mr. Sisi had made a humiliating concession to a wealthy ally.

The ruling on Tuesday was largely unexpected, because the Egyptian judiciary has long been considered to be deferential to — and, critics would say, complicit with — the country’s leadership. In recent years, Egyptian judges have sentenced hundreds of government critics to lengthy prison sentences or even death.

If upheld, the verdict will not only leave Mr. Sisi in awkward position with an important ally, but it could also pave the way for the prosecution of senior government officials, legal experts warned. Under Egyptian law, officials who negotiate with foreign governments deals that harm national interests can be sentenced to life in prison. Some legal experts suggested that this possibility, despite being politically slim, was why the agreement was signed by the prime minister rather than by the president, who traditionally seals such deals.

If tried as individuals who compromised Egyptian territory, officials could face the death penalty.

On the other hand, if the agreement is upheld, it will be sent to Parliament, where many expect it to be approved quickly given the wide support for Mr. Sisi there.

Mr. Sisi’s government had presented the transfer of the islands as part of an economic deal that would help stimulate Egypt’s economy, but critics took to the streets in protests that led to the arrests of nearly 1,300 people, human rights defenders say.

By Tuesday evening, the Egyptian government announced that it had filed an appeal. Officials in Riyadh, the Saudi capital, did not comment immediately.

In response to the wave of criticism, Mr. Sisi had urged Egyptians to stop discussing the deal, saying it was signed in secret to avoid attention in the news media. “Please, I don’t want anyone to talk about this anymore,” he said in a speech in April.

Critics had taken to referring to Mr. Sisi as Awaad, a character in an old Egyptian song who sold his land — an act traditionally considered shameful.

Essam el-Eslamboly, one of the Egyptian lawyers who filed the case challenging the transfer of the islands, called the ruling “a victory for the judiciary,” because it demonstrated that the courts “are fair and only care about the interests of the country.”

During testimony before the court, Mr. Eslamboly said, the government’s lawyers declined to furnish documents to make their case, arguing instead that the transfer was a “sovereign decision” that was made at the discretion of the cabinet, and one that the administrative court had no right to question.

“The silence revealed the weakness of their position,” he said.

Legal experts, both for and against the deal, have described the crux of the official argument in court as hard to prove, at best.

“There is no legal text that defines what constitutes a ‘sovereign decision,’” said Mohamed Hamed el-Gamal, the former chief of the Council of State, the judicial body that issued Tuesday’s verdict. “It is really up to the judges here,” he said, adding that there was no legal text immunizing such decisions. “That idea is based on judicial norms; normally such decisions are respected,” he said. “But such decisions are not usually challenged either.”

A version of this article appears in print on , on Page A9 of the New York edition with the headline: Egypt Court Blocks Plan for 2 Islands. Order Reprints | Today’s Paper | Subscribe