Spanish officials today hailed a visit by Premier Felipe Gonzalez to Morocco as a "breakthrough" in the nations' often vexed relations.

The two-day visit, Gonzalez's first official trip abroad, was viewed as a key test of the new Socialist administration's effort to defuse tensions with the kingdom across the Straits of Gibralter.

On return early today, Gonzalez said his visit had "cleared clouds on the horizon" and was the basis of "fruitful cooperation" between Spain and Morocco. A close aide of the premier said, "It was a greater breakthrough than we had dared imagine."

Spanish officials stressed that Gonzalez spent an hour an half with King Hassan II in Fez--45 minutes longer than scheduled. The monarch had then unexpectedly sent his crown prince to attend a luncheon hosted by Gonzalez for Moroccan ministers--a move interpreted by the officials as one of marked respect for the Spanish leader.

Gonzalez's reception appeared to be a preview of close links in the short term. Officials here said Hassan, Moroccan Premier Maati Bouabid and Foreign Minister Mohammed Boucetta were scheduled to visit Madrid separately in the near future.

The improved relationship was underscored by the apparent Moroccan decision not to raise the potentially inflammatory issue of the Spanish enclaves of Ceuta and Melilla. The two garrison towns on Morocco's Mediterranean coastline were conquered by Spain in the 16th century and are claimed as an integral part of Spain.

Gonzalez, in turn, said, "We wanted to create a relationship of confidence with Morocco and I think we've succeeded." A significant element in Gonzalez's endorsement is his refusal to back the independence struggle of the Polisario Front, which has been engaging Moroccan forces in the Western Sahara since the former Spanish colony was ceded to Rabat in 1975.

While in opposition, Gonzalez and his Socialist Party supported the Polisario, viewing it as the sole political representative of the Western Sahara, and were hostile to what was characterized as a feudal and oppressive monarchy in Morocco.

Since coming to power in December, the Socialist administration has made stability in northwest Africa a priority. This has meant uncritical support for King Hassan. The independent Madrid newspaper El Pais said Gonzalez's approach to Morocco marked the change from an opposition politician to a national leader aware of strategic considerations.

The groundwork for a switch on support of Polisario had been laid last week during a four-day trip by Spanish Deputy Premier Alfonso Guerra to Algeria, where he met Algerian and Polisario leaders.

Western diplomatic sources said Guerra, who is Gonzalez's closest political adviser, was briefed by Algerian officials on a new closer relationship between Algeria and Morocco at the expense of the Polisario. That organization, the sources said, is seen by both Rabat and Algiers as increasingly dependent on Libya.

The diplomatic sources said Gonzalez in Morocco had traded his former support for the Polisario Front for a Moroccan low-profile approach to the Ceuta and Melilla issue. The garrison towns have a high symbolic value for the Spanish Army, and the Socialist administration is eager to reassure the military that the enclaves are safe. "Both parties have gained," said a diplomatic source.

The new neighborly relations paved the way for renewed talks on other issues, which include a fishing agreement, the transportation of Moroccan citrus products through Spain and the welfare of Moroccan nationals in Spain.

Special correspondent Jim Rupert added from Rabat:

Diplomatic sources in this Moroccan capital stressed that Hassan, who has built his domestic consensus around the issue of "reunification" of Morocco, cannot surrender claim to Ceuta and Melilla.

The Moroccan decision not to raise the issue during Gonzalez's visit was testimony to its desire not to alienate or embarrass the Spanish government, they said. These sources said Morocco had strategic and economic interests in keeping good relations with Madrid but there was no real relaxation by Rabat on the Ceuta and Melilla issue.