Catholics should reject selfishness and intolerance to help fashion a new world, Pope Francis said in a Sunday sermon that drew 3 million people and ended a week-long pilgrimage to Brazil in which he emphasized the need to renew the 2,000-year-old church and bring it closer to the people.

Speaking from an elaborate stage on Rio’s famed Copacabana beach, the 76-year-old pontiff told young people from nearly 180 countries, who had come for the church’s biennial World Youth Day, to fearlessly go and spread the word of God.

“Follow these words: Serve without fear,” Francis said, on his first foreign trip since becoming pope in March. “Bringing the Gospel is bringing God’s power to rip out and raze evil and violence, to destroy and demolish the barriers of selfishness, intolerance and hatred to build a new world.”

Conveyed in Catholic imagery, it was an appeal for young people energized by the gathering to draw closer to a church that religious analysts say has grown out of touch and lost followers by the millions in recent years, a trend readily apparent in this continent-size country where Catholicism once dominated.

Inspiring ordinary people and his own clergy was a vital part of Francis’s mission.

The day before, in a Saturday meeting with bishops, the pontiff stressed that outreach must be done in more simple, accessible language and in a more energetic way. Using unusually blunt terms, he spoke of the exodus of Catholics and of a church that is a “prisoner of its own rigid formulas.”

“I would like all of us to ask ourselves today, ‘Are we still a church capable of warming hearts?’ ” the pontiff said.

But during his visit, in events large and small in the shadow of the iconic Christ the Redeemer statue on Mount Corcovado, the pope was focused on more than just drawing lapsed Catholics back to the fold.

Francis also stressed that the church will be a forceful advocate of social justice and fair play. In a visit to a slum Thursday, he said the rich must do more to narrow vast social inequality. He told followers not to become disillusioned by corruption, talked about public frustration with crime and spoke of the importance of dialogue in resolving disputes.

The pope met with prison inmates and drug addicts, and he heard confessions in a Rio park alongside priests from around the world. To the consternation of his security service and Brazilian officials, the pope broke protocol by wading into crowds and, while riding in a nondescript Fiat sedan, rolling down the window to touch followers and kiss babies.

For Ana Paula Santos, 24, who works the register at a store and came from Brazil’s Minas Gerais state to see Francis, it was a lesson in humility that to her demonstrated how the pope is living the teachings of Jesus.

“My faith has grown strong now,” she said, “and I want to embrace this faith even more.”

John Thavis, author of “The Vatican Diaries,” a look at power and politics in the Catholic Church, said Catholics the world over have heard a lot about the pope’s pastoral touch and his agenda on social issues since he replaced Benedict XVI.

“This week, we saw it on display in Brazil, especially in encounters with young inmates, drug addicts, the sick and the poor,” Thavis said. “He showed by example what it means to be a more open and evangelical church. I think he was also trying to show how the Catholic Church can stem the departure of its own faithful by responding more directly to their spiritual needs.”

Thavis said he found it telling that Francis, despite spending seven days in the world’s biggest predominantly Catholic country, never explicitly mentioned abortion, birth control, sexual permissiveness or gay marriage, issues that have divided Catholics and prompted some to sever their ties to the church.

Instead, Thavis said, Francis focused on bridge-building themes. “That’s consistent with this pope’s approach,” he said, “and it contrasts with Pope Benedict’s visit to Brazil in 2007, when he touched on all those topics.”

The pontiff appealed to youthful rebelliousness, telling a group of young people from his native Argentina to “make a mess” upon returning to their parishes — meaning he wants them to do away with complacency.

“What is it that I expect as a consequence of World Youth Day? I want a mess. We knew that in Rio there would be great disorder, but I want trouble in the dioceses!” he said Thursday. “I want to see the church in the street. I want to get rid of the mundane, the comforts, clericalism, this closing ourselves off in the parishes, the schools or institutions.”

The pope’s emotional and seemingly spontaneous pronouncements helped attract hundreds of thousands of young people to Copacabana’s 2.5 miles of crescent-shaped beach day after day.

The beach, which normally attracts sunbathers, bikini-clad beauties and volleyball players, was outfitted with giant screens so that no matter how far the faithful were from Francis, they could see Sunday’s service. And along the white sand, young pilgrims from across Latin America and as far away as Africa pitched tents and slept in sleeping bags, in an outing that was part jamboree, part religious revival.

The 3 million who attended Sunday’s service dwarfed the 1 million who attended Mass at the last World Youth Day in Madrid. The Associated Press reported that only Pope John Paul II’s 1995 Mass in Manila, which attracted 5 million people, was larger. Among those here were Brazilian President Dilma Rousseff, Argentine President Cristina Fernández de Kirchner and other regional leaders.

Paulina Krippel, 27, a teacher from Chile, said the pope’s words left her enthusiastic about returning home and following the church’s teachings.

“The message that touched my heart is that the young must be revolutionary,” Krippel said. “The young are the ones who can bring about change, who have idealism. They can make a revolution, especially related to injustice in Latin America.”

Guadalupe Tarqui Ramos, 17, from Bolivia, said she will leave Brazil with the intention of “passing on my faith and things I learned here to my friends and to my family.”

Ramos said the pope inculcated her with a sense that young people can be joyful while working as “agents of change in the world, under the guidance of God.”

“This was so important to me,” she said, “because my way of thinking changed.”

Paula Moura contributed to this report.