In anticipation of a major fight against Israeli-backed forces in southern Lebanon in the next few weeks, feuding Lebanese Moslem factions agreed here tonight on a compromise plan designed to end their recent internecine confrontations.

Extensive fighting for control of predominantly Moslem west Beirut last week led to the resignation of Prime Minister Rashid Karami and the apparent collapse of his government. But he participated in tonight's agreement and now seems willing to continue at his post.

The decision was announced in a communique late tonight after two days of meetings between the Lebanese Moslem religious and political leaders and top Syrian officials. According to participants, the Syrians emphasized the need for Lebanese unity as the final stage of Israel's withdrawal from Lebanon approaches. The intent is to resist Israeli attempts to establish a border buffer patrolled by primarily Christian Lebanese it has armed.

The Syrian argument for unity, Lebanese Druze leader Walid Jumblatt said earlier in the talks, was "forget about west Beirut because we are expecting big events in the Bekaa and Sidon."

Jumblatt said that if Israeli-armed Christian militias attempt to assert control over the Bekaa valley south of the strategic lookout post at Mount Baruk as Israel pulls back, there will be "terrible bloodshed."

The communique tonight emphasized the need for a united Lebanese government, including Christians as well as Moslems, and called for an end to the sectarian divisions of Lebanese politics.

Syria has consistently pressed for the formation of a nonsectarian, unified Lebanese government. Syria has demonstrated a decisive influence on Lebanese politics in the past two years, and it has an estimated 40,000 troops stationed in Lebanon. But even for Syria, achieving a semblance of unity among the Lebanese Moslems required the concoction of a complex and still unstable formula for patrolling the contested streets of west Beirut.

Under tonight's agreement, the Green Line corridor separating Moslem and Christian sectors of Beirut is to be abolished and security measures taken citywide by an as yet unspecified "security force."