MEXICO CITY — A former labor minister and novelist from the governing party was decisively elected president of Costa Rica in a runoff on Sunday, holding off an evangelical Christian singer who had built an upstart campaign in part on his opposition to same-sex marriage.

With ballots from most polling stations counted, the former minister, Carlos Alvarado Quesada, won three-fifths of the vote, while his opponent, Fabricio Alvarado Muñoz, received the rest, according to the federal election authorities.

The overwhelming victory defied polls in recent weeks that showed the candidates locked in a statistical dead heat or Mr. Alvarado Muñoz with a lead.

“Costa Rica once again delivered a beautiful democratic message,” Mr. Alvarado Quesada, 38, told supporters late Sunday night. “Well done, Costa Rica.” He added: “What unites us is much greater than what divides us!”