Three surviving members of West Germany's notorious Baader-Meinhof terrorist gang-including the founder and leader, Andreas Baader-committed apparent suicide early today in their maximum security jail cells in Stuttgart only hours after West German commandos foiled an attempt to free them by a daring attack on a hijacked Lufthansa airliner.

The two episodes have dealt an astonishing and sudden double setback to West Germany's ruthless terrorist underground that just a few days ago seemed to be more powerful than it had been since the early 1970s.

The news early this morning from Justice Ministry officials that Baader, 34, and Jan Carl Raspe, 33, had shot themselves through the head and that 37-year-oldGudrun Ensslin had hung herself, bewildered and relieved West Germans just as had the news a few hours earlier that 36 hostages aboard the Lufthansa jet had been freed in Mogadishu Somalia, after the Commando attack.

Another imprisoned gang member, Irmgard Moeller, 30, also attempted to kill herself with a bread knife today. She was rushed by helicopter to a nearby hospital and tonight was reported to be no longer in critical condition.

As chruches throughout West Germany announced they would hold thanksgiving services tonight, public relief over the commando exploit in Somalia was punctured by questions surrounding the mysterious circumstances under which today's reported suicides were carried out.

State Justice Ministry offiicals this afternoon were unable to explain how the prisoners got handguns in their solitary confinemnt cells, or how they managed to find out about the thwarted hijack attempt that was meant to free them, or how they managed to coordinate their suicides. Just two weeks ago, a new anti-terrorist law was passed that legalized the total isolation of the prisoners from each other and from any contact with the outside world, including their lawyers.

Skepticism was immediately voiced by Baader's former lawyer, Hans Christian Stroebete, who questioned whether the three terrorists could have committed suicide and who said the entire affair seemed "very doubtful."

Another lawyer, Otto Schilly, who helped defend the Baader-Meinhof terrorists, suggested that the deaths may not have been suicide, a potentially volatile claim that the Bonn government is going to great lengths to refute by bringing in expert medical examiners from Switzerland. Austria and Belgium as well as inviting lawyers and representatives of Amnesty International.

The commando exploit clearly lifted West German's spirits and also boosted the fortunes of Chancellor Helmut Schmidt's government. The conservative opposition has argued generally that Schmidt's centerleft coalition has been too soft on the roots of terrorism. Like the Israeli raid at Entebbe, Uganda, last year that freed Israeli hostages taken from a hijacked Air France jetliner, the Mogadishu operation seems to have given a shot in the arm to Schmidt's thin majority government that has suffered a series of political misforturnes for West German security.

On the one hand, terrorists still at large may seek revenge in still more violent attacks upon West Germany's generally orderly and prosperous society and its "establishment" figures.

Probably more important, the deaths of Baader, Ensslin and Raspe today remove from official custody the central figures in whose names much of the terrorism that has plagued this country in recent years has been carried out.

The Baader-Meinhof gang grew out of the radical student movement in the 1960s. It later took a sharp turn toward violence, spreading terrorism and a fear of anarchism through many West German cities and that has been continued sporadically in recent years by their followers in the extreme leftist "Red Army Faction."

The hijackers of the Lufthansa jet this week, the kidnappers of industrialist Hanns-Martin Schleyer in Septermber, the kidnap-murder of banker Juergen Ponto in August, the attack on the West German Embassy in Stockholm in 1975 and numerous other acts were all carried out by terrorists seeking to blackmail the Bonn government into releasing Baader and his comrades.

Without this target, and without the symbolic leadership that Baader is believed to have continued to exercise from his jail cell, the West German terrorist movement has suddenly been dealt a stunning, although perhaps not disabling blow.

Their deaths also follow a recent crackdown against a small group of radical lawyers who allegedly were supporting terrorist activities by carrying messages between jailed guerrillas and their comrades on the outside and even helping to mastermind some of the attacks.

The prison deaths mean, in effect, that the entire imprisoned leadership of the hard-core elements of the Baader-Meinhof gang have died by their own hand, according to police.

Ulrike Meinhof, co-founder and intellectual leader of the original gang also was found hanging in her Stuttgart cell last May in what the government also said was a suicide.

In November, 1974, another major gang figure, Holger Meins, died of a hunger strike protesting conditions in the same fortress-like wing of the Stuttgart Prison specially built to hold the Baader-Meinhof band.

Three dead prisoners and Moeller were among the 11 terrorists that the hijackers of the Lufthansa jet had demanded be freed and flown to safety out of West Germany as the price for not blowing up the aircraft.

The "Red Army Faction" terrorists who kidnapped Schleyer more than six weeks ago also had demanded the release of the 11. The three deaths today plus the foiled hijack attempt in which three of the four hijackers were killed, may now seal Schleyer's fate if he is still alive.

After three dead prisoners were serving life terms after having been convicted in April this year of a string of robberies, bombings and killings, the worst of which was in May, 1972, when four U.S. soldiers were killed and 14 others injured in bombings at Frankfurt and Heidelberg.

State Justice Minister Traugott Bender told reporters today that Raspe was found shot and dying in bed with a pistol lying next to him as a guard was bringing him breakfast. Guards immediatley checked other cells and found Baader already dead with a pistol along side him and Ensslin hanging from a window frame.

The prisoners' rooms are inspected daily. Bender said there was only speculation about how it happened, although he claimd the prisoners frequently talked about contemplating suicide.

Why have so many apparently comitted suicide?

Sociologists here have given several reasons, including the "martyrdom" that extremists may feel comes with it and emotional instability caused by lengthy solitary confinement. Most important now, however, may be the assessment, after the unsuccessful hijacking, that Bonn would never release them no matter how many lives are at stake.

Although today's deaths in some ways mark the end of an era, police authorities have recently calculated that there are still some 1,200 likely terrorists around and perhaps five times that many sympathizers; people who will help rather than commit crimes.

The lingering phenomenon posed by the Baader-Meinhof gang remains West Germany's biggest dilemma: why do so many of these young radicals come from middle or upper class homes and out of good universities and turn up as anarchists in a country that has risen to unprecedented levels of prosperity and democracy since World War II

Ulrike Meinhof was an art history student and well known editor of a left-wing underground newspaper. Raspe was a practicing sociologist and author of several scientific papers before he became a demolitions expert in 1970. Gundrun Ensslin, the daughter of a respectd Protestant minister, is a highly educated woman who studied German and English philosophy and who spend a year at an American high school in Warren, Pa.

Ironically, it was Baader, who was a high school dropout, who seemed to have a knack for leadership and a spell over some of the female gang members, especially Ensslin.