Flanked by banners of the banned Solidarity trade union, tens of thousands of Poles celebrated mass here today after marching from all corners of the country in annual pilgrimages fed by nationalism, religious fervor and political alienation.

Poland's Roman Catholic primate, Cardinal Jozef Glemp, called for national unity in a sermon outside the Jasna Gora monastery, which contains Poland's shrine of the Black Madonna. Church officials estimated that more than 200,000 persons attended.

Beneath Glemp on an iron fence facing the pilgrims were dozens of red-and-white banners mixing the themes of the Solidarity union, devotion to Catholicism and ongoing resistance to efforts by Communist authorities to stabilize the country. "Black Madonna -- Hope of Our Enslaved Nation," read one.

Church leaders, warned by the government against allowing the pilgrimage to be overtaken by politics, unsuccessfully sought to prohibit banners and other opposition demonstrations from the mass and marches by an estimated 165,000 from cities across the country.

Glemp, who in the past has used sermons at Czestochowa to call for freedom for political prisoners, avoided any direct mention of politics today, speaking instead of his hopes for a future without conflict. "The divisions in Europe and in our country will not divide us here" at the shrine, he said. "Instead, we will be led into unity."

The primate's moderate tone signaled the church's efforts to avoid new conflicts with the government of Gen. Wojciech Jaruzelski after a year of turbulent relations, several observers said. Glemp and Jaruzelski met for the first time in 18 months earlier this summer.

For many pilgrims, however, the ceremonies at Jasna Gora and treks of 200 miles or more through the countryside proved a reaffirmation of the alienation they feel from Poland's nominal authorities five years after the strikes that created Solidarity.

"I'm 53 years old and the pilgrimage is difficult for me in physical terms," said a journalist from Warsaw who walked 175 miles to the shrine in 10 days with a group of 5,000 persons. "But I felt as though I were in the real Poland. Everyone was praying and saying what they wanted. No one was afraid."

Along the pilgrimage route from Warsaw to Czestochowa, which wound through dozens of small villages off the main roads, thousands of students and intellectuals offered prayers for Solidarity leader Lech Walesa and President Reagan and listened to accounts by former political prisoners of interrogation by security forces, participants said.

At regular intervals, the pilgrims also prayed for "forgiveness" for Communist Party leaders and asked for "courage on Oct. 13," the day of scheduled parliamentary elections for which Solidarity's underground leadership has called a boycott.

"Our pilgrimage was on the theme of the conquest of evil by good," said Stanislaw, a pilgrim who traveled with the Warsaw University group. "And by evil we understood communism."

The defiant tone was as traditional as the pilgrimages themselves, which began from Warsaw in 1711 during an epidemic. The 600-year-old Jasna Gora monastery and its icon, a painting of Mary, the mother of Jesus, adorned with jewels and blackened by smoke from candles, has been a symbol of Polish nationalism since 1655, when a besieging Swedish army was defeated by outnumbered Polish defenders.

Poles expressed nationalism by walking to Czestochowa during the 19th-century rule of the country by foreign powers and during the World War II Nazi occupation. Recently, the size of the pilgrimages has steadily grown despite occasional efforts by authorities to discourage or stop them.

This year, the Pauline monks who maintain the shrine said that groups from every major Polish city and 18 countries arrived in Czestochowa during the last four days. The largest group, from Warsaw, left the capital with about 30,000 members on Aug. 6 and had swelled to 38,700 by the time it reached this town yesterday.

Grouped according to membership in churches or social clubs, many of the pilgrims repeat the walk year after year. "I couldn't get through a year without being able to do it," said Adam, a 30-year-old engineer who hiked the 140 miles from the western city of Wroclaw for the seventh time. "There is not much freedom in Poland, but here, we can show it."

This year, the principal obstacle was bad weather. Driving rain soaked many of the groups and flooded their routes for four consecutive days last week. This week, the rains were replaced by blazing sunshine.

The marchers, their clothes and tents having been soaked by the downpours, said that farmers and entire villages took them in each night, fed them and offered them bedrooms or barns to sleep in. "Nobody feels tired," said a 42-year-old woman from Warsaw. "People are different than in everyday life. They are full of sacrifice, full of helping one another."

For many, the seven to 10 days of 20- and 30-mile hikes served principally as a personal and spiritual release. "I have the feeling of being born again," said Eva, a 30-year-old sociologist who has completed 10 pilgrimages. "I leave everything behind me and stop thinking about my work, about my daily problems. The only important thing is to carry out the pilgrimage."

Others, however, saw a general message for the nation. "This is a demonstration of power and of strength that gives more hope to the whole country," said a 19-year-old from Wroclaw. "Because here we show we feel free."