By Haitham Ahmed

CAIRO (Reuters) - An Egyptian court on Tuesday annulled a maritime border accord with Saudi Arabia that would have seen Egypt lose control of two Red Sea islands, in a setback for President Abdel Fattah al-Sisi.

The maritime demarcation accord, announced in April, caused public uproar and prompted rare protests in Egypt where many people say they were taught at school that Tiran and Sanafir were Egyptian.

The timing of the announcement, during a visit to Cairo by the Saudi king that coincided with the signing of aid deals, created the impression among many that the islands were sold.

As anger rose, Sisi made an impromptu speech denying the islands were sold and urging Egyptians to end the debate. But a group of lawyers, including former presidential candidate Khaled Ali, challenged the agreement in court.

On Tuesday, Judge Yehia al-Dakroury ruled that Egyptian sovereignty over the islands holds and could not be amended in favor of another state.

Tiran and Sanafir lie between Saudi Arabia and Egypt's Sinai Peninsula at the narrow entrance to the Gulf of Aqaba leading to Jordan and Israel.

Saudi and Egyptian officials say they belong to the kingdom and were only under Egyptian control because Saudi Arabia asked Egypt in 1950 to protect them.

Ali argued that according to a 1906 maritime treaty between Egypt and the Ottoman Empire, the islands are Egyptian. The treaty precedes the founding of Saudi Arabia in 1932.

The government said it would appeal the verdict.

"The government is studying the reasons for the ruling and will ... challenge it at the higher administrative court of the State Council and request that ... it be canceled," Magdy al-Agaty, minister of legal and parliamentary affairs, said.

The demarcation agreement was also due to be discussed by parliament in the coming weeks. Two parliamentarians said the debate would go ahead and take into account the verdict.

It was not clear whether the government could activate the accord if parliament approved it but the higher administrative court did not.

Saudi Arabia and other wealthy Gulf Arab states have showered Egypt with billions of dollars in aid since Sisi toppled President Mohamed Mursi of the Muslim Brotherhood in 2013 following mass protests against his rule.

But a sharp drop in oil prices and differences over foreign policy issues such as the war in Yemen have raised questions over whether strong Gulf Arab support can be sustained.

Egyptians are eager for economic revival after years of political upheaval. But the islands issue hurt national pride, prompting thousands of protesters to take to the streets in April chanting "people want the fall of the regime", a slogan from the Arab Spring uprisings of 2011.

More than 200 people were arrested in connection with protests over the islands. At least 85 have since been acquitted but more than 150 have been handed jail sentences or fines, judicial sources said.

(Reporting by Haitham Ahmed; writing by Amina Ismail; Editing by Lin Noueihed and Janet Lawrence)