Pope Francis celebrates Brazil Mass on Copacabana beach

Pope Francis has closed one of the biggest Roman Catholic youth festivals with a Mass on Copacabana beach.

More than three million people are estimated to have gathered for the service in the city of Rio de Janeiro.

Many of the pilgrims had slept on the beach after attending an all-night vigil, also led by Pope Francis.

Sunday's ceremony marks the end of the Pope's five-day visit to Brazil - his first foreign trip since becoming pontiff in March.

He flew back to Rome late on Sunday.

The BBC's Wyre Davies in Rio says almost every inch of the two-and-a-half mile long beach was occupied ahead of the Mass, as most of the young people stayed after the vigil, pitching tents or sleeping in the open.

The vigil and Sunday mass attracted the biggest crowd ever to Copacabana beach, which has hosted rock concerts, sports events and the traditional New Year celebrations.

Pope Francis encouraged young Catholics to get out of their parochial boundaries in order to help others.

"Go and don't be afraid of serving," he said.

"Jesus did not gather the Apostles so they lived in isolation. He called them so they formed a group, a community," said Pope Francis.

He announced the next Catholic youth festival would be held in the Polish city of Krakow in 2016.

Female activists held a demonstration nearby in support of abortion and women's rights.

But our correspondent says the Pope and the Church hierarchy will be delighted at the huge turnout and the way Francis has been received by pilgrims from across the globe.

Preaching in favelas

In the past three decades, the Catholic church has lost millions of followers to smaller Christian denominations.

In his vigil address on Saturday, the pontiff had urged the pilgrims not to be "part-time Christians", but to lead full, meaningful lives.

Rio's famous stretch of beach has been dubbed "Popacabana"

Speaking on a huge stage at the beach where a mock church structure was built, Pope Francis referred to the street protests which have been taking place in Brazil for more than a month.

"The young people in the street are the ones who want to be actors of change. Please don't let others be actors of change," he told the crowd at the vigil.

"Keep overcoming apathy and offering a Christian response to the social and political concerns taking place in different parts of the world."

Also on Saturday, the Pope repeated his challenge to fellow Roman Catholic clerics to take to the streets.

In a speech to 1,000 bishops and clerics in Rio's cathedral, he said they should go to the favelas - Brazil's shanty towns.

"We cannot keep ourselves shut up in parishes, in our communities, when so many people are waiting for the Gospel," he told the audience.

Protests, sometimes violent, broke out in cities across Brazil last month against corruption, poor public services and the high cost of events like the 2014 World Cup.

Crowds chanted "Long live the Pope" and threw gifts, including football jerseys, when he arrived for Sunday's Mass

The Vatican hopes the pontiff's popularity in Brazil will help reverse the trend of people leaving the Catholic Church

A mock church structure was erected on Rio's iconic Copacabana beach for the World Youth Day festival

But the huge crowds proved too much for some pilgrims who required medical attention.

Many had slept on the beach after an all-night vigil also led by Pope Francis

Rio's mayor said more than three million people packed Copacabana beach on Saturday night

The Pope watched a performance during the vigil service

The World Youth Day cross was fixed to the stage ahead of the Pope's arrival