As the drama in the Grand Mosque of Mecca drew to a muffled close earlier this week, preliminary assessments of Saudi Arabia's first internal upheaval for more than a decade suggest that the oil-rich kinddom will soon see a shift in power away from the nation's religious leaders and toward the more progressive elements in the royal family.

For some months now, Saudi watchers have discerned a struggle between conservative religious fundamentalists, who wished to move the country back toward militant Islamic precepts, and political moderates, headed by Crown Prince Fahd, who have a more realistic view of modern statecraft. Until last week it was the conseervatives who appeared to be gaining ground. Alarm at the continuing rise of Ayatollah Khomeini in neighboring Iran and despondency at the continuing paralysis of the Camp David agreement were weakening the moderates' position.

All this changed once the events in Mecca began to unfold. Within the holy city of Mecca, power is shared between its royal governor, Prince Fawaz, and its spiritual head, Abdulaziz bin Bazz, a blind and saintly octogernarian who is the chief of the ulemas (holy men), the nearest Saudi Arabian equivalent to an archbishop or an avatollah. The leaders of the obscure sect that attacked Mecca's sacred Haram (shrine) were well known to bin Bazz. He had pleaded for clemency on their behalf only a few months earlier after they had been convicted on charges of sedition, plotting against Crown Prince Fahd and other pre-terrorist offenses. Despite opposition from the professional security experts in the Ministry of Interior, King Khalid bowed to the pressure from the ulemas and paroled in January the very gunmen who were to lead the onslaught on Mecca in November.

As these facts became known during the second and third days of the occupation of the Grand Mosque, the public's reaction was hostile. There were not exactly demands for bin Bazz's resignation -- they don't do things that way in Saudi Arabia -- but the general feelin g of outrage against the sacrilegious attackers spilled over into direct criticism of the ulemas for being soft on criminals.

Contrary to suggestions from the Ministry of Information in Riyadh that the intruders were a small and ragged band of half-crazed vandals, the attacking force in fact consisted of over 300 heavily armed men using well-rehearsed military tactics. Equipped with walkie-talkies, machine guns adn bazookas, they moved into strategic positions during the confusion of a well-attended dawn prayer ceremony on the first day of the Moslem New Year. Within minutes of their initial assault, they had positioned snipers on the commanding heights of the minarets, set up a command post in a first floor scripture room, erected a surprisingly solid defense barricade at the Mosque entrance and captured about 50 hostages.

The immediate seriousness of the situationn rocked the Saudi leadership. Initially nobody knew who the attackers were, how they had got their arms,how much support they might have in other parts of the country or what their ultimate objectives might be. Rumors involving ransom demands, coups d'etat and every imaginable outside influence from Khomeini to the CIA flew around the bazaars. It therefore came as a great relief when the terrorist leader, a 22-year-old styling himself mohammed bin Abdullah, merely demanded that all true believers should now proclaim him as "the Mahdi," or new messiah.

From this point onward, the Saudi authorities handled the situation with considerable skill and effectiveness. The key members of the royal family who took control of the crisis were Crown Prince Fahd and his two brothers, Prince Nayef, minister of the interior, and Prince Sultan, minister of defense. Prince Nayef's intelligence sources swiftly established that "the Mahdi's" followers were a dissident Moslem religious group largely drawn from the Al Oteiba tribe. Their extremist views had long been known, but had been dismissed as harmless on the advice of the ulemas.

This was a grave misjudgement, for the Mahdists had been plotting their attack for many months and had obtained their formidable arms from sympathetic military officers with access to the arsenal of the National Guard. The skill with which the Mahdists used their weapons made the Saudi army's counterattack a difficult military operation. It was complicated further by a delay caused by the necessity of consultations between Crown Prince Fahd and of the army's returning the terrorists' fire within the holy confines of the Grand Mosque. Eventually some 30 ulemas signed a decree permitting military operations to proceed. These were personally directed by Defense Minister Prince Sultan and by all accounts were restrained yet successful, since all hostages were extracted unharmed and heavy shelling forced the Mahdists into an early surrender with less than 20 casualties.

Other members of the royal family who won praise wre Deputy Interior Minister Prince Ahmed and the governor of Riyadh, Prince Salman. They had the task of keeping the situation calm and under tight security control in sensitive areas of the kingdom. Prince Ahmed in particular took over emergency command in the Eastern Province, which was potentially vulnerable not only because all the oil fields are located there, but also because the area contains a heavy concentration of Shia Moslems,who were feared to be potential supporters of the Mahdi. Those fears proved groundless. Indeed, one of the outstanding features of the crisis was the solidarity of popular support for the royal family's handling of the situation.

Looking ahead, the Saudis are likely to learn some useful lessons and to gain much valuable experience from what mercifully turned out to be only a mini-crisis. Those ministries that performed well, Defense and Interior, can expect to be strengthened in coming months and to be particularly active in their surveillance of the hitherto unwatched fringe religious groups that have been encouraged to proliferate by the ulemas. Above all, the royal family may be encouraged by recent signs of public opinion to conclude that their people care a lot more about internal security than they do about religious innovation. If Saudi democracy is not yet on the way in, at least theocracy may be slowly on the way out.