President Francois Mitterrand's Socialist Party lost four parliamentary by-elections to the conservative opposition in France today, an embarrassing setback in the first electoral test since Mitterrand came to power last summer.

The losses, in districts whose parliamentary elections were invalidated because of technical irregularities, still leave the Socialist government with a comfortable majority of more than 280 in the 491-seat National Assembly. Nevertheless, they represent a highly symbolic political defeat that could slow the momentum of the administration eight months after its sweeping victory.

"The state of grace is really over," said Bernard Pons, head of the main rightist opposition party, Rally for the Republic. "This is not a simple reaction due to dissatisfaction over certain things, but rather an overall rejection of the government's policies."

The election results marked the second slap at Mitterrand's Socialist government in as many days. France's highest court, the Constitutional Council, ruled Saturday night that several key parts of Mitterrand's nationalization program are unconstitutional, causing an estimated delay of three weeks while the laws are redrawn to meet the court's objections.

Neither setback threatens the government's hold on power or its ability to carry out its nationalization platform. But together they give the rightist opposition valuable weapons in the political battle that has been bitter but one-sided since Mitterrand's election in May followed by the Socialist sweep in Parlimanent in June.

"These results are harbingers of the great disillusion which will be brought on by the Socialist-Communist experiment founded on an alliance against nature and economic and social policies that are inappropriate and dangerous," said the second major opposition group, the Union for French Democracy.

All four districts--two in Paris, one in a Paris suburb and one in the Marne region east of Paris--traditionally have voted conservative. Three of the four voted for Mitterrand in May and sent Socialist candidates to Parliament in June. Thus, while the outcome today was a sharp reversal of the Socialist victory, it was also a return to customary voting habits.

Political leaders from the Socialist majority and the rightist opposition had been equally reluctant to herald the elections as a test of national strength. French political analysts said the hesitation reflected opinion polls that showed that all four contests were close. They predicted-- rightly--that only the winning side would read national implications into the vote once it was over.

At the same time, political stars of both camps turned out to campaign for their candidates. Mitterrand, in keeping with his office, stayed away from the rallies and streetside buttonholing. But Prime Minister Pierre Mauroy, his ministers and Socialist Party Secretary General Lionel Jospin showed up to urge election of Socialists.

Similarly, Jacques Chirac, mayor of Paris and paramount opposition leader, was active for his Rally for the Republic candidates.

Former president Valery Giscard d'Estaing, who has been almost silent since his defeat, made his first major political appearance last week at campaign headquarters for Jacques Dominati in the second electoral district in Paris. Tonight he sent a congratulatory telegram to Alain Peyrefitte, his former justice minister and victor in the Seine-et-Marne area on the outskirts of Paris.

"I congratulate you, my dear minister, for the testimony of confidence and esteem that the voters of Seine-et-Marne have given you by your brilliant election," Giscard wrote. "The work of the National Assembly will benefit from your cultivation and your sense of public welfare."

Peyrefitte is a member of the prestigious French National Academy of intellectuals and is the author of a half-dozen books. A minister six times under three presidents, he is particularly disliked by the Socialist majority. His major work as justice minister under Giscard, a tough anticrime law entitled "Liberty and Security," is being repealed by the current administration as repressive.

He defeated Marc Fromion, 28,532 votes to 22,151. The Socialist Fromion was elected in June largely on the strength of urban voters, particularly Communists, who stayed away today, French analysts said.

Dominati, with Giscard's boost, defeated Pierre Dabezies, a "leftist Gaullist" who won in June with Socialist support, 9,591 to 6,363. In the two other races, Pierre Benouville, a Rally for the Republic conservative who won in June, defeated his Socialist opponent, Stelio Farandjis, by 12,002 to 8,135. Bruno Bourg-Broc of the Rally for the Republic defeated the Socialist elected in June, Annette Chepy-Leger, by 27,569 to 22,860.

The clear-cut victories mean that no second round will be necessary. In explanation, analysts pointed out that both the left and the right had fielded single candidates.