The state has the right to appeal the ruling at a higher court, and the accord must also be approved by parliament.
The agreement, which was announced in April, caused uproar and protests in Egypt where many say they were taught at school that Tiran and Sanfir islands were Egyptian.
The ruling is a setback for President Abdel Fattah al-Sisi, who had asked Egyptians in a speech to end to the controversy over the islands deal, which was announced during a visit to Cairo by the Saudi king.
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.
Saudi Arabia and other wealthy Gulf Arab states showered Egypt with billions of dollars in aid after 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 an 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 calling for the “fall of the regime”, a slogan associated with the 2011 Arab Spring uprisings.
More than 200 people were arrested in connection with protests over the islands controversy. Many have since been freed.