Turkish Prime Minister Bulent Ecevit resigned today following an astonishingly severe defeat by right-wing opposition forces in midterm parliamentary elections.

The resignation leaves the way open for a resumption of power by former prime minister Suleyman Demirel.

"Our government has lost a majority in parliament. The election results as they reflect in parliament necessitate a change of government," Ecevit told reporters on emerging from a Cabinent meeting.

With Turkey in a state of deepening, often violet confusion, the election results heighten political uncertainty in this troubled NATO-member nation, whose strategic value to the West has been enhanced by this year's turn of events in neighboring Iran.

Demirel's Justice Party won all five lower house seats at stake in the elections from Ecevit's left-of-center Republican People's Party. The Justice Party also took 33 of the 50-Senate seats being contested, according to provisional results. The ruling party won only 12.

The votes of the combined right-wing opposition now surpass those of the government in both the upper and lower houses of parliament. Ecevit's government polled only 28 percent of the vote, 20 percent behind the tally gained by the main opposition Justice Party.

Demirel, who was ousted from office in 1977, called on the prime minister to resign -- or face a vote of no confidence when parliament reconvenes on Nov. 1.

Ecevit is expected to hand over his government's resignation in a meeting with President Fahri Koruturk Tuesday morning, according to the Cabinet source.

The election reflected widespread disillusionment with Ecevit's unsuccessful efforts both at ending a wave of political violence that has left more than 2,000 dead in two years and at easing Turkey's economic crisis.

Clashes between right-wing and left-wing extremists have been compounded by religious and ethnic confrontations. This state of unrest if being kept alive by economic difficulties -- including a 70 percent inflation rate and a 20 percent unemployment rate.

Starved of imported materials, most factories are running at low capacity. There are long lines for such essentials as margarine and gas, and many Turks have gone for months without cooking oil, light bulbs and even their beloved thick coffee.

Earlier this year the Ecevit government received pledges of up to $1.45 billion in credits from the major industrialized powers to help the Turkish economy. But to win these, Ecevit agreed to a number of stringent internal measures dictated by the International Monetary Fund -- a fact which the opposition played on by portraying Ecevit as a beggar and the Western financial institutions as interfering in Turkish affairs.

It is doubtful that a new Demirel government could do much more in the near term to settle Turkey's chaos. Demirel himself is reportedly hesistant to make a comeback just yet, apparently afraid of being swept away by the problems that were Ecevit's undoing. His own party is not strong enough to rule without the support of the two far-right parties, the National Salvation Party and the Nationalist Movement Party.

"As soon as this government goes we will discuss what the next one should be," he said noncommitally.

Demirel is believed to favor an interim government which would take unpopular measures without political risk, and which would then be followed by early general elections, which were not due until the summer of 1981.

But this will not be easy, to engineer and the country could well relapse into a period of prolonged political uncertainty.

The rivalry between Ecevit and Demirel is every bit as personal as it is institutional. The leaders of Turkey's two largest political parties intensely dislike each other -- a situation that has only quickened the country's slide to its current desperate pass.

Ecevit, an accomplished poet, is the son of a university professor and a painter. He came into politics from journalism. Demirel was born to peasant parents, earned an engineering degree and ran a successful business representing foreign companies. Both men are intelligent and aggressive, but their different backgrounds have produced an unbridgeable gulf.

One other looming power that further complicates the Turkish political mix is the military. The generals have intervened twice in the past 20 years. But having accomplished little, they show no inclination at the moment to try again.