Socialist Prime Minister-designate Bettino Craxi today informed President Francesco Cossiga that he had reached a compromise with four other political parties to end the four-week political crisis caused by his resignation June 27.

The compromise will give Italy a coalition government nearly identical to the one Craxi had presided over for almost three years, a political longevity record in postwar Italy.

According to the details of the compromise, the 52-year-old Craxi will rule until next March, when he will leave the government and return to head his Socialist Party for the remaining two years of the current parliamentary term.

Socialist party officials confirmed today that when Craxi does step down, a Christian Democrat will take over the prime ministership and receive the necessary support from the Socialists and the other three parties in the coalition, until the parliamentary term expires in 1988.

While Craxi won his gambit to remain in power despite a bitter campaign by Christian Democratic party leader Ciriaco de Mita to unseat him, his victory is limited.

Next spring, de Mita will be able to put one of his own in power, most probably Craxi's Christian Democratic Foreign Minister Giulio Andreotti or Deputy Prime Minister Arnaldo Forlani.

"There were no victors and no losers," declared Forlani after the deal was struck in a meeting between Craxi and de Mita.

The Italian government has been paralyzed for the past four weeks because of Christian Democrat demands that someone from their party, which is the largest in the country, be allowed to head the government after three years of Socialist rule. Craxi opposed the Christian Democrats on the grounds that they had unilaterally sought to alter the coalition pact for no reason but that it was their turn to rule.

The strong-willed, often autocratic prime minister maintained that his government had given Italy its largest period of political stability since World War II and, as a result, had been a major factor in Italy's economic recovery and growth in recent years.

With every political poll indicating the Italian people favored him over any other candidate for the prime ministership, Craxi insisted that he should not be forced out of power.

Craxi originally resigned June 27 after having lost a secret vote on a relatively minor local financing bill. Though he did not have to submit his resignation because of the vote -- he had lost dozens of similar votes in the preceding month -- he chose to resign as a protest against coalition politicians who he said were pretending to be his allies on floor votes while voting against him on secret ballots.

Craxi, who was appointed caretaker prime minister until the coalition could be brought back together, expected to force a quick new mandate from his coalition partners and an agreement to do away with the sort of parliamentary secret votes that had so embarrassed him.

Instead, the Christian Democrats used the resignation as an excuse to advance their demands for an "alternanza," or alteration, of rule in the coalition.

Just what sort of government Craxi ends up with next week is still a question mark. He is to have a summit meeting with his coalition partners next Tuesday to thrash out those details and a new coalition program for the final two years of the legislature.

After the summit, it will then be up to President Cossiga to either refuse to accept Craxi's original resignation (which was held in abeyance) and send the same government back to Parliament for a vote of confidence, or to allow Craxi to appoint a new, somewhat altered coalition government.