The Polish government and striking workers signed a sweeping agreement today that ended an 18-day labor crisis and contained concessions by authorities that were unprecedented in a communist-ruled country.

State television and radio gave full coverage tonight to emotional ceremonies endorsing the agreement in the port cities of Gdansk and Szczecin, the two main centers of the strike that had paralyzed Poland's Baltic Coast.

The strike leaders were shown shaking hands with government negotiators and both sides joined in singing the Polish national anthem.

Cheering workers in both cities, where more than 200,000 had joined the walkout, said they would return to work Monday morning. Striking workers elsewhere in the country are expected to return to work in the next few days under terms simimlar to those reached on the Baltic coast.

The major victory for the workers was an agreement approved by the ruling Communist Party Central Committee allowing establishment of independent trade unions which will have the right to strike.

Final agreement on the remaining demands among the 21 originally made by the strikers came today. They included a number of political and economic reforms including limitations on government censorship and greater freedom of expression as well as the freeing of several prominent dissidents who had been arrested in connection with the strike.

The agreement marks the first time that a Soviet Bloc country has granted its workers the right to form unions independent of the ruling Communist Party and granted the right to strike. At the same time, however, the workers have pledged to continue to accept the Communist Party as the highest authority.

At the Lenin Shipyard in Gdansk, strike leader Lech Walesa said: "We have not won everything that we hoped for and dreamed about. But we have achieved as much as we could under the circumstances, including respect for certain civil rights."

Noting that work would resume on Sept. 1, the anniversary of Hitler's invasion of Poland, he added, "This is a day when we think of our homeland . . . Just as we have shown solidarity during the strike, so too will this solidarity be maintained as we go back to work."

The agreement to end the strike in Gdansk, involving about 500 factories, almost collapsed late Saturday night after a rift developed within the presidium of the strike committee. Some members were unhappy over a pledge that the new independent trade unions would respect the leading role of the Communist Party in Poland's political system.

But Walesa, 38, who has emerged as an immensely skilled handler of his followers, saved the settlement by appealing over the heads of the presidium to the full committee of about 1,000 delegates. In a rousing speech, he shouted: "We have formed these trade unions ourselves. If you are there inside them, as I am, then you can be sure that we won't allow anybody else to have a leading role over them."

On censorship, Walesa said that the journals of the new trade unions would publish whatever they wanted, whether anybody liked it or not. The strike paper. Solidarity, will become the new union newspaper.

Another point of near collapse came over the detention in Warsaw of at least 25 leading dissidents, including leaders of the Workers' Defense Committee and the pro-Catholic Movement for the Defense of Human and Civil Rights. Finally the chief government negotiator, Deputy Premier Mieczyslaw Jagielski, promised to raise the issue with the state prosecutor.

Walesa replied, "If they're not let out soon, we'll go back on strike . . . and now, we have the right to strike."

The list included Jacek Kuron and Adam Michnik of the dissident Workers' Defense Committee.

This evening a spokeswoman said that five members already had been released and the rest would probably be freed in the morning. "This is a great victory," she said, speaking by telephone from Kuron's apartment were the committee has organized an information service on the strikes.

Earlier, 140 prominent Polish intellectuals signed a statement calling for the release of all political prisoners, particularly those arrested recently. They said their continued detention contradicted promises made at last week's session of the Communist Party ycentral Committee for a better social and political atmosphere.

In the final negotiating session at the Lenin Shipyard, Jagielski gave the government position on each of the strikers' 21 demands. Most important were demands for independent trade unions and the right to strike, which he conceded.

He also agreed to pay increases of up to 1,000 zlotys ($34) a month, higher family allowances, improved supplies of basic foodstuffs including meat, and a better medical service. He promised that censorship would be relaxed with the exception of state and military secrets and hostile propaganda against Poland.

After the formal signing ceremony, Jagielski said: "These negotiations show how we Poles should talk to each other. There were no losers and no winners in this dispute . . . We all wish to serve our socialist Poland."

In the signing ceremony, which took place at Szczecin on Saturday, Deputy Premier Kazimierz Barcikowski made the point that the agreement reached there could serve as a model for the solution of similar conflicts in the future. Although the new unions appear to be restricted to the Baltic Coast at present, their establishment elsewhere has not been ruled out.

Huge crowds gathered outside the gate of the Gdansk shipyard this morning for Sunday mass and the final round of negotiagions. There was frenzied cheering as the government commission agreed to the strikers' demands. f

When Walesa appeared at the gates, the crowd said "Sto Let, Sto Let -- "May He Live 100 Years" -- a song which also greated Pope Paul II on his triumphant pilgrimage to Poland last year.

The formal signing cremony took place in the huge committee hall where negotiations have been conducted over the last 17 days, first with the shipyard director and later with the government negotiators. Both Walesa and Jagielski signed each page of a thick sheaf of documents.

Throughout Poland, large crowds gathered round televisions and radios to see and hear broadcasts of the signing ceremonies. Many Poles expressed pleasure at the government approval of independent trade unions.

The present round of labor unrest was sparked off by a rise in meat prices July 1. It gradually assumed increasingly political overtones, but it was not until the strike at the Lenin yard on Aug. 14 that the strikers demanded independent unions.

The government also promised not to harass strike leaders and agreed to the construction of a monument in honor of workers killed by police during previous strikes in Gdansk 10 years ago.