WARSAW, MAY 28 -- Lech Walesa, the labor union leader who has orchestrated a decade of political change in Poland, succeeded early today in saving the Solidarity-led government from a potentially disastrous nationwide rail strike.

In a midnight meeting with strike leaders, Walesa, whose judgment and political worth have aroused public suspicion lately, also reestablished his credentials as a national resource.

The strike settlement appeared likely to boost Walesa's on-again, off-again campaign to replace Gen. Wojciech Jaruzelski as president of Poland.

Walesa slipped away late Sunday night from his home in Gdansk and traveled to the nearby Baltic Coast city of Slupsk, where he persuaded rail workers to end an eight-day strike that had closed Poland's sea ports and idled trains across the northern half of the country.

At Walesa's urging, the rail workers also suspended -- for two weeks -- plans for a nationwide rail walkout. The Ministry of Transport said freight and passenger rail service should return to normal within two days.

"On our road to democracy, there are various dangerous bends. The railway men's protest was one of them," a triumphant Walesa said today. He had warned earlier that the strike could lead to anarchy and civil war.

On Sunday, the rail workers, who are demanding a 20 percent wage increase, called for a 90-minute national demonstration strike that was to have occurred today. They also threatened an open-ended countrywide strike beginning Tuesday unless the government responded to their wage demands.

The rail workers had posed the most serious challenge so far to the Solidarity government that came to power last fall. Its leader, Prime Minister Tadeusz Mazowiecki, had responded by categorically ruling out higher wages for strikers.

Mazowiecki argued that any strike-forced wage increases would prompt similar demands from other workers and would wreck a five-month-old economic recovery program that has sharply reduced inflation.

The program has won applause and attracted billions of dollars of aid from Western donors, but it has also triggered mass unemployment and slashed living standards in a country that is already one of the poorest in Europe. The rail strike was, as Mazowiecki conceded, a sign of public fatigue and impatience.

The strike threat coincided with -- and overshadowed -- the first fully free elections in Poland in half a century. Turnout in Sunday's election, in which more than 50,000 officials were chosen for local governing bodies, was a disappointing 42 percent. That is exactly 20 percentage points lower than the turnout for last year's parliamentary election, which set the stage for the end to Communist rule. The Solidarity newspaper Gazeta Wyborcza suggested today that the low turnout was a sign of the unpopularity of changes taking place here.

While Mazowiecki and other officials in Warsaw have presented a stony face to the strikers, refusing to discuss any wage demands, Walesa has been more sympathetic. He insisted that the demands of the rail workers in Slupsk, who want $10 more than the average monthly wage of $98, were just. He said it was only the strikers' "means" that were wrong.

Walesa's unexpected session with the strikers Sunday night was his second straight all-night negotiation. A meeting in Slupsk in the early morning hours of Saturday ended in failure, and the Nobel Prize-winning Solidarity chairman had gone home in apparent defeat. It was the first time since 1980, when Walesa helped found the independent labor union, that he seemed to have failed to control a strike action.

That apparent defeat meshed with Walesa's recent spotty performance in other areas. Things have not gone well for him since his brilliant success last summer, when he outmaneuvered the Communist leadership and put together the first non-Communist government in the East Bloc. Then, Walesa chose not to take a position in the government.

Instead, he returned home for a long, restless winter in Gdansk. During that time, his popularity in opinion polls fell well below that of Mazowiecki, whom he had hand-picked as prime minister.

Over the past six months, Walesa's statements have been contradictory, alarming and often incomprehensible. His union colleagues accused him of running Solidarity like a dictator. Twice he urged the government to bypass parliament and adopt new laws by decree. He scolded the government for allowing Soviet troops to stay in Poland and for failing to purge remaining Communists.

Over the past two months, Walesa declared frequently that he wanted to be president of Poland. But just as frequently, he declared that he did not want to be president. Here in Warsaw, intellectuals and technocrats complained that Walesa was a loose cannon. Several suggested that the unschooled Gdansk shipyard electrician had outlived his usefulness.

On Sunday -- in a move that appears to have startled and gladdened people across Poland -- Walesa made good on that promise. By consistently urging the government to take the interests of working people into account, he also appears to have maintained a measure of credibility among militant labor leaders.

"This matter goes down in history, and we will draw conclusions from the history," Walesa said today, referring to the strike settlement.

One obvious conclusion is that Walesa's hugely energetic and charismatic persona remains the glue that holds Poland together.