PHILADELPHIA — Pope Francis turned penitent and pastor Sunday on the final day of his visit to the United States, declaring himself “overwhelmed by shame” at the sexual violation of children by his clergy, embracing inmates at a local jail, urging young people to leave the loneliness of social media and bidding farewell with a huge downtown Mass.

Since arriving in the United States from Cuba on Tuesday during his 10-day tour, Francis, 78, had been met by large crowds — tens of thousands during a drive through Central Park in New York, in Madison Square Garden, at a canonization in Washington; perhaps several hundred thousand on Saturday night for a potpourri of prayer, musical performances and testimonials at a festival for the Vatican-sponsored World Meeting of Families.

Sunday’s Mass, marking the end of the meeting, on Eakins Oval in front of the Philadelphia Museum of Art, presented the pope on the grandest of scales, with nearly a million passing through the city’s lockdown-emptied streets into Benjamin Franklin Parkway, which was studded with Jumbotron screens.