BOGOTÁ, Colombia — A Colombian peace deal that the president and the country’s largest rebel group had signed just days before was defeated in a referendum on Sunday, leaving the fate of a 52-year war suddenly uncertain.

A narrow margin divided the yes-or-no vote, with 50.2 percent of Colombians rejecting the peace deal and 49.8 percent voting in favor, the government said.

The result was a deep embarrassment for President Juan Manuel Santos. Just last week, Mr. Santos had joined arms with leaders of the Revolutionary Armed Forces of Colombia, or the FARC, who apologized on national television during a signing ceremony.

The surprise surge by the “no” vote — nearly all major polls had indicated resounding approval — left the country in a dazed uncertainty not seen since Britain voted in June to leave the European Union. And it left the future of rebels who had planned to rejoin Colombia as civilians — indeed, the future of the war itself, which both sides had declared over — unknown.