DUBLIN — Ireland became the first nation to approve same-sex marriage by a popular vote, sweeping aside the opposition of the Roman Catholic Church in a resounding victory Saturday for the gay rights movement and placing the country at the vanguard of social change.

With the final ballots counted, the vote was 62 percent in favor of legalizing same-sex marriage, and 38 percent opposed.

The turnout was large — more than 60 percent of the 3.2 million eligible voters cast ballots, and only one district out of 43 voted the measure down. Cheers broke out among the crowd of supporters who had gathered in the courtyard of Dublin Castle when Returning Officer Riona Ni Fhlanghaile announced around 7 p.m. that the ballot had passed, 1,201,607 votes to 734,300.

Not long ago, the vote would have been unthinkable. Ireland decriminalized homosexuality only in 1993, the church dominates the education system, and abortion remains illegal except when a mother’s life is at risk. But the influence of the church has waned amid scandals in recent years, while attitudes, particularly among the young, have shifted.