President Obama on Friday eulogized the Rev. Clementa Pinckney, one of the victims in last week’s church shooting in Charleston. After speaking for about 30 minutes, he closed his remarks by leading mourners in a powerful chorus of “Amazing Grace.”

Thousands of people paid their respects to Pinckney, who was killed with eight others at the historic Emanuel AME Church.

At the conclusion of his eulogy, Obama spoke of a “reservoir of goodness,” a phrase used by the writer Marilynne Robinson. “The reservoir of goodness,” he said. “If we can find that grace, anything is possible. If we can tap that grace, everything can change. Amazing grace. Amazing grace.”

After a long pause, he surprised the mourners at the ceremony: He started singing.

By the second bar of Obama’s hymn, the mourners were on their feet and applauding. By the line “saved a wretch like me,” the organ had joined him, followed by hundreds of voices.

After a strong, soulful rendition of the song, Obama shouted, “Clementa Pinckney found that grace.” He repeated the sentence, naming each of the nine victims.

“Through the example of their lives, they have now passed it on to us,” he said. “May we find ourselves worthy of that precious and extraordinary gift as long as our lives endure. May grace now lead them home.”

The Christian concept of grace was a theme Obama returned to many times during the speech, before the stirring conclusion.

“This whole week, I’ve been reflecting on this idea of grace. The grace of the families who lost loved ones.  The grace that Reverend Pinckney would preach about in his sermons,” he said earlier in the eulogy, which took on topics including gun violence and the Confederate flag. “According to the Christian tradition, grace is not earned.  Grace is not merited.  It’s not something we deserve.  Rather, grace is the free and benevolent favor of God, as manifested in the salvation of sinners and the bestowal of blessings.”

He continued, “As a nation, out of this terrible tragedy, God has visited grace upon us, for he has allowed us to see where we’ve been blind. He has given us the chance, where we’ve been lost, to find our best selves. We may not have earned it, this grace, with our rancor and complacency, and short-sightedness and fear of each other — but we got it all the same.”

As the sight of a president crooning a gospel hymn powerfully communicated, the eulogy gave Obama a chance to touch on his deep personal roots in the black church.

“Our pain cuts that much deeper because it happened in a church,” he said of the killing of Pinckney and his congregants. “The church is and always has been the center of African American life, a place to call our own in a too often hostile world, a sanctuary from so many hardships.”

He discussed the history: “Over the course of centuries, black churches served as ‘hush harbors’ where slaves could worship in safety; praise houses where their free descendants could gather and shout hallelujah; rest stops for the weary along the Underground Railroad; bunkers for the foot soldiers of the Civil Rights Movement.”

He continued, “They have been, and continue to be, community centers where we organize for jobs and justice; places of scholarship and network; places where children are loved and fed and kept out of harm’s way, and told that they are beautiful and smart, and taught that they matter. That’s what happens in church.”

Want more religion coverage? Follow Acts of Faith on Twitter or sign up for our newsletter.