Stanislaw Kania took firm control of Poland's Communist Party tonight as the party congress overwhelming elected him over friendly opposition in the Soviet Bloc's first free and secret balloting for a party leader.

The election of Kania, who replaced discredited party leader Edward Gierek in September, came a day after the congress stunned the nation by turning out most of the party's leadership in electing a new policy-making Central Committee.

Kania's victory and the defeats yesterday of many of his rivals left the former internal security chief in much firmer control of the country, which has undergone a year of social and political turmoil with repeated warnings from the Soviet Union and other Warsaw Pact countries against deviation from socialist orthodoxy. As of late tonight, there was no official Kremlin comment on Kania's election.

In a victory speech, Kania promised to stick to his centrist line of gradual reform, which he has dubbed "socialist renewal."

He said his most important task was to restore the trust of the Polish people in the Communist Party and the trust of the world in Poland. Describing himself as "a simple soldier of the party" who never expected to be elected first secretary, he said the qualities demanded of his leadership would be "courage, decisiveness, common sense, and modesty."

Kania, 54, defeated his only rival, Kazimierz Barcikowski, by 1,311 votes to 568. The two candidates both supporters of democratization of Poland's Communist Party, applauded and hugged each other, and Kania received a standing ovation from the packed congress hall.

Kania and Barcikowski are considered allied representatives of the party's centrist stream. Two other prominent politicians, Stefan Olszowski and Mieczyslaw Rakowski, who represent the party's conservative and liberal wings respectively, also were asked to run for election but declined. They appeared to have no chance of winning in any case.

Before the voting, both candidates faced lengthy questioning from the delegates on their records, particularly in the 1970s when they both held senior positions under Gierek's now discredited leadership.

Kania was chosen party leader last September after what amounted to a political coup against Gierek. As the party's secretary for organization (a post Barcikowski now holds), he had been responsible for the day-to-day handling of the labor crisis the previous month.

Since his election as party leader, he has shunned dramatic gestures and worked behind the scenes to gain a steadier grip on the levers of power.

He has led the party from the center, trying gently to nudge it in a reformist direction in response to the demands of Solidarity, the independent labor union federation formed last summer. But he also has been responsive to pressures from the Soviet Union and the entrenched party bureaucracy.

His position has been challenged several times during the past year, most notably last month when the Soviet Communist Party sent a toughly worded letter to the Polish Central Committee criticizing his leadership. Hard-liners in the Polish party used the occasion to try to get rid of him. But Kania survived while his main challenger, Tadeusz Grabski, was dropped from the Central Committee yesterday.

The unprecidented process of screening candidates and voting has dominated this congress, which was convened in an attempt to restore the party's shattered morale and heal its divisions. This has provoked comment, both in Poland and elsewhere in Eastern Europe, that not enough has been done to find ways of overcoming the country's grave economic problems.

In fact, away from public attention, the delegates have divided into 16 working groups to tackle subjects ranging from economic reform to ideology. The results of these commissions will become clear only at the end of the congress, which is likely to last at least until Monday.

An analysis of the Central Committee elections shows that the most startling change is the defeat of the political apparatus and its replacement by workers and farmers. At the last Central Committee elections in February, which were manipulated by Gierek, government and senior party officials won 62 percent of the seats while workers and farmers won only 28 percent.

This time the proportion was dramatically reversed, with officials winning 8.8 percent and farmers and workers 54 percent.

One of the delegates, Dominik Horodynski, editor of the cultural weekly Kultura, said the results had surprised even those persons inimately involved in party life.

"The changes are very far-reaching," he said. "Now we have a Central Committee in which farmers and workers are in the majority while experienced politicians are a minority."

Much depends on whether the party's inexperienced new elected leaders can assert their will over the huge bureaucracy. The new Central Committee members have already expressed their intention of playing an active role.

Horodynski noted that, under new party statutes, the position of the Central Committee would be strengthened in relation to the Politburo and Secretariat -- the two executive bodies that traditionally are the real source of power in a communist country.

Elections to the Politburo and Secretariat are expected to take place Sunday, thus completing the changeover of power.

Among the candidates who lost their posts on the Central Committee was the prosecutor-general, Lucan Czubinski, whose investigation into alleged police violence in the northern town of Bydgoszcz was criticized by some delegates. The Bydgoszcz affair, in which three Solidarity activists were severely beaten, almost provoked an open-ended general strike in March.

Kania came from a peasant family and began his climb through the party bureaucracy as a result of activity in the party's youth organization after World War II. He joined the Central Committee as a deputy member in 1964 and became a full member four years later. He was appointed a Central Committee secretary shortly after the workers' riots in December 1970.

In 1971, he was named a candidate member of the Politburo and he became a full member in 1975.