The French Foreign Ministry announced tonight that Iraq has agreed to pardon two Iraqi opposition activists whose release had been demanded by Islamic extremist groups holding eight French hostages in Beirut.

A Foreign Ministry statement said that the opposition activists, both Shiite Moslems, had been freed from house arrest and told that they were free to leave Iraq if they wished. France expelled the two men to Baghdad in February after a wave of bomb attacks here that were blamed on pro-Iranian extremists.

Political analysts here said that the Iraqi gesture took some of the political heat out of France's hostage crisis just four days before crucial parliamentary elections here. The Islamic Jihad group, which has claimed to have killed one French hostage already, had threatened to "execute" another by Sunday unless the two Iraqis were released.

Today, however, in a further act of violence against France in Lebanon, Beirut police said a French Army captain serving with the United Nations peace-keeping force was killed by sniper fire outside the French ambassador's residence in mostly Moslem west Beirut. The unidentified officer was the seventh French U.N. observer killed in the last two years while monitoring cease-fire violations in Beirut.

According to French officials, President Francois Mitterrand had appealed to Iraqi President Saddam Hussein for the release of the two Iraqis in a personal message transmitted by a senior French diplomat. The French government sent a plane to Baghdad tonight to bring the two back to France.

While leaders of France's right-wing opposition have shown caution in their public comments about the government's handling of the French hostages in Lebanon, most commentators believe that the dramatic foreign policy setbacks of recent days can only harm the Socialist government's chances in Sunday's election. According to French press reports, Interior Minister Pierre Joxe offered to resign after public criticism of the expulsions of the two Iraqis to Baghdad, but his resignation was rejected by Mitterrand.

The reported execution of one of the four Frenchmen kidnaped last year and last weekend's seizure of a four-man television crew have provoked enormous popular concern here, overshadowing the final days of the election campaign. Other campaign issues, largely revolving around the economy, have been pushed into the background as press and television devote saturation coverage to the hostages.

Today, broadcasting networks staged a minute's symbolic silence in sympathy with the hostages. Demonstrations of support for the hostages outside the National Assembly in Paris were attended by political and trade union leaders from both left and right.

In interviews with French news organizations, the wife of the hostage allegedly killed by Islamic Jihad, Michel Seurat, accused Joxe of sealing her husband's fate by expelling the Iraqis. She also ridiculed statements by Prime Minister Laurent Fabius that France would not give in to blackmail over the remaining hostages.