MADRID — Pope Benedict XVI closed the religious ceremonies of World Youth Day on Sunday with a giant Mass in which he told young people to “swim against the tide” and abide by the principles of the Catholic Church despite broader changes in society.

The culmination of World Youth Day — a six-day event that drew more than one million pilgrims to Madrid — took place at an air base on the outskirts of the city.

Though the event was marred last week by clashes between the police and protesters condemning its cost, the huge and ebullient welcome for the pope provided a powerful demonstration of his influence, even at a time when church attendance has been dwindling in Roman Catholic countries like Spain.

At the end of Sunday’s Mass, the pope announced that the next such event would be in Rio de Janeiro in 2013. Until then, he told those at the service, in Portuguese, that they “will be swimming against the tide in a society with a relativistic culture, which wishes neither to seek nor hold on to the truth.”