A vast sea of people, perhaps 2 million in all, bade farewell to Pope John Paul II today at his last mass in Poland, and the pontiff beseeched them to "never lose your spiritual freedom."

Later, weary and tearful, the pope addressed Polish government officials at an airport departure ceremony and said his nine-day visit was "an act of courage on both sides."

"Our times needed this act of courage," the pope said to Polish President Henryk Jablonski. "Sometimes one should take a risk and go in directions that had not been taken before."

Then he embraced the Communist Party official warmly and was on his way.

These were scenes of particular poignancy for the pope, the former archbishop of Krakow who was once again leaving behind his home, his country and his people. Throughout a long emotional tour, an abiding devotion to Poland came through in all John Paul's remarks.

"Before I leave you," he declared this morning from a gleaming altar draped in white cloth, "I wish to give one more look at Krakow. This Krakow in which every stone and every brook is dear to me. And I look once more on my Poland."

[The pope spoke in a similar vein when he arrived in Rome, where he was met by Italian Premier Giulio Andreotti, news services reported. "The pope has visited his native land," John Paul said, "and now has returned to Rome where the Lord has posted him.I thank God that I was able to see Poland again."]

The crowd that had converged overnight for the mass in Krakow filled a vast meadow near the city center. The air was filled with hymns and clapping when the pope arrived in his specially designed van with a raised platform. The vehicle moved smoothly through the throng like a boat on water while the pontiff waved affectionately.

In his sermon, the pope spoke of faith, Christian principles and pride in his Polish heritage. Drawing to a close, he said, "I beg you once again to accept the whole of the spiritual legacy which goes by the name 'Poland' . . . I beg you never to lose your trust. Do not be defeated; do not be discourage; do not on your own cut yourselves off from the roots from which we have our origins . . . and never lose your spiritual freedom, with which 'He makes a human being free.'"

At those words, the crowd began prolonged rhythmic applause that lasted several minutes and drowned out the pope's final phrases.

This double meaning - religious devotion and political freedom - marked the whole of John Paul II's stay here in a Communist-ruled country that has now showed beyond any question or doubt that its loyalties to the church - and especially to this pope - are profound.

Traveling here last weekend, the pope told reporters on his plane that he was determined to keep his emotions in check. "I must dominate myself," he said, and for most of his stay the pope conveyed a sense of joy and conviction in urging worshipers to insist on full religious liberty.

Finally, today at the airport, his emotions broke loose. He wept openly as a mountaineer band played a parting song. Turning to Jablonski and Poland's Roman Catholic primate, 78-year-old Cardinal Stefan Wyszynski, John Paul said softly, "This visit has sapped all my energy."

"But your visit has given your homeland strength," Wyszynski said in response.

As he had when he arrived, the pope kissed the ground. He passed from one church leader to another giving each a last private message. But it was his comment to the Polish president, the country's ceremonial chief of state, that perhaps was the most significant.

The government has made it clear that it wants to be identified in the minds of Poles with the success of the journey. Officials have swallowed whatever concern they have about the overhelming enthusiasm for the church, which represents an ideology sharply at odds with the Marxism-Leninism that is formal state doctrine here.

"We are very pleased that everything went so well," one senior party official commented today, summing up the general view.

There were no major disturbances, he said, and while the pope expressed his views firmly on sensitive matters, they never strayed over the line to incite political turmoil.

In the comming days, full analyses of the visit in Communist Party newspapers should give a sharper picture of just what the state believes has happened here since the pope arrived June 2. "It isn't just enough to say how delighted they are," a young Pole observed. "They must surely want to say something more."

Today's final - and largest - mass, like all those that proceeded it, was notable for order and efficiency. Acres of buses were parked in the suburbs while pilgrims walked in parish groups following "pilots" holding crosses to show the way. Some people walked for eight hours to reach the scene.

As they waited for the mass to start, a priest's voice spoke soothingly over a loudspeaker assuring the people that all was well and giving them arrangements for dispersing at the end of the ceremony. This stress on order was an important element throughout the pope's stay and he dwelled on it again today in his homily.

There is, the pope said, a "Christian moral order . . . From every victorious test, moral order is built up. From every failed test moral disorder grows.

"We know very well from our entire history that we must not permit absolutely and at whatever cost, this disorder.For this we have paid a bitter price many times." CAPTION: Picture 1, An enormous crowd gathered in Krakow yesterday for the final mass of Pope John Paul II's nine-day trip through Poland, his Communist-ruled homeland. AP; Picture 2, The pope gestures to reporters and photographers. UPI; Picture 3, Pope John Paul II kisses the ground of his native Poland in a fond farewell. UPI