Polish authorities formally agreed today to the establishment of a rural branch of the independent trade union movement Solidarity, and striking farmers ended their occupation of two government buildings in northern Poland.

The agreement was signed after allnight talks between a government team led by Labor Union Affairs Minister Stanislaw Ciosek and representatives of the country's private farmers in the northern town of Bydgoszcz. It removes a major source of political tension in Poland.

But it also confronts Poland's communist authorities with a unique new institution that presumably would become a bargaining agent for Poland's 3.5 million private farmers on matters ranging from pricing and distribution to agricultural commodities to allocation of credits, fertilizers and equipment.

In signing the agreement, the Polish communist leadership has departed significantly from practices followed in all other communist countries. Not only do private farmers own three-quarters of total agricultural land in Poland, but they also are allowed to form a nationwide organization outside communist control.

The new union, which already claims to represent at least half of all Polish private farmers, will be formally registered as a legal organization by May 10. Until then, the agreement stated that the union known as Rural Solidarity will be allowed to operate as if it were legal.

The union's chairman, Jan Kulaj, 23, hailed the agreement as a "great victory" for farmers following a six-month legal and political battle. In February, Rural Solidarity's application for registration was rejected by the Supreme Court on the grounds that only hired employes had the right to form a union.

Under the terms of today's agreement, a proposed trade union law will be amended to cover private farmers as well as industrial workers.For their part, the farmers have agreed to recognize the leading role of the Communist Party and cooperate with state-run rural organizations.

The final agreement came just in time to enable the striking farmers to rejoin their families for the Easter break. Most of the farmers are deeply religious, and their cause was helped by expressions of strong support from Poland's powerful Roman Catholic Church.

Church officials said earlier that, if agreement was reached in time, a special "Hallelujah" would be sung in churches throughout Poland on Easter Sunday.

After signing the agreement with Ciosek, Kulaj said: "We have reached our goal. At first they refused to allow us to operate as a union -- but at last we've been recognized. The problem is now definitively solved and we can leave this building with honor."

Nearly 100 farmers had been occupying the Bydgoszcz headquarters of the procommunist United Peasants' Party for the last month. On Thursday, the protests spread to the nearby town of Inowroclaw, where farmers, supported by local workers, began a hunger strike.

Communist Party officials originally voiced strong opposition to the registration of Rural Solidarity and party leader Stanislaw Kania asserted that there was no place for "political opposition" in the countryside. But the party has now accepted it as inevitable in view of the urgent need to increase agricultural production.

The Bydgoszcz farmers said recognition of their union, along with previous agreements guaranteeing continuity of land tenure and increased prices on agricultural commodities, would help improve morale in the countryside. But, in talks with a visitor, they predicted that it could take up to five years before order is restored to Polish agriculture.

Farmers have complained previously that they were discriminated against since the government gives priority to the state farms in the distribution of vital agricultural supplies such as fertilizers and feedstuffs. d

Private farmers claim they are more efficient than the large state farms.

They therefore argue that agricultural production would rise if scarce resources such as fertilizers were diverted from the state sector to the private.

In its program for economic recovery, the government apparently has accepted the logic of this argument. But the private farmers are skeptical of promises and declarations, which is why they have insisted that their interests be represented by a union.

The farmers also see Rural Solidarity as having an important role to play in negotiating higher prices for food. In the past, attempts by the government to raise food prices caused widespread industrial unrest. This could be avoided if the two Solidarity unions were to talk directly