Pope Francis concluded a youth festival by urging young people to go forth and build a new world at a mass on Copacabana Beach in Rio de Janeiro.
The crowd attending the event was estimated by organisers and the Vatican to be over three million people.
Pope Francis's message to the young people in Rio for week-long World Youth Day festivities, sometimes called "the Church's Woodstock," was serious: they should not make their time in Rio a one-off experience.
In his sermon during the Mass from a huge white stage at the beach's northern tip, he said they should return to their home countries energised and ready to work for social change.
Many people had slept in the area and local residents poured out of homes and buses, turning out to see the Argentine pope on the final day of his week-long trip.
Aerial television footage showed the sand and pavements blanketed with people for several kilometres along the crescent-shaped shoreline.
The throng of people, many in the green and yellow Brazilian colours, gave Francis the kind of ecstatic welcome that he has received all through his trip to his home continent.
They shouted and sang as he was driven through the crowd in an open-sided popemobile.
He stopped often to kiss babies offered to him by their mothers on the shoreline most famous for its bars and nightclubs and hedonist spirit.
Brazilian President Dilma Rousseff, Argentine President Cristina Fernandez, Bolivian President Evo Morales and several Latin American vice presidents were among those who attended.
The Copacabana events were to have taken place on a pasture on the outskirts of Rio, but days of unseasonable rain turned the area into a field of mud.
Pope Francis, who was due to leave for Rome tonight after addressing Latin American bishops, has dedicated much attention in his speeches to the problems, the prospects and the power of youth.
He announced that the next World Youth Day will be in Krakow, Poland, in 2016.
Last night, he encouraged Brazil's young people, who have protested against corruption in their country, to continue their efforts to change society by fighting apathy and offering "a Christian response."
Brazil, Latin America's largest nation and still the world's most Catholic country despite declining numbers of faithful, was rocked by protests against corruption, the misuse of public money and the high cost of living.
On Friday night he urged them to change a world where food is discarded while millions go hungry, where racism and violence still affront human dignity, and where politics is more associated with corruption than service.
The day before, during a visit to a Rio slum, he urged them to not lose trust and to not allow their hopes to be extinguished. Many young people in Brazil saw this as his support for peaceful demonstrations to bring about change.