CARACAS, Venezuela — President Hugo Chávez, long a fiery foe of Washington, won re-election on Sunday, facing down cancer and the strongest electoral challenge of his nearly 14 years in office and gaining a new mandate to deepen his socialist revolution.

Though his margin of victory was much narrower than in past elections, he still won handily. With 90 percent of the votes tallied, Mr. Chávez received 54 percent, to 45 percent for his opponent, Henrique Capriles Radonski, the national election council said. Fireworks erupted in Caracas after the news, and Chávez supporters celebrated in the streets.

Shortly before 11:30 p.m. local time, Mr. Chávez stepped out onto the balcony of the presidential palace in Caracas and waved to a sea of jubilant supporters. “My words of recognition go out from here to all who voted against us, a recognition for their democratic temperament,” he said. A former soldier, he called the election a “perfect battle.”

Still, after a spirited campaign, the polarizing Mr. Chávez finds himself governing a changed country. He is an ailing and politically weakened winner facing an emboldened opposition that grew stronger and more confident as the voting neared, and held out hope that an upset victory was within reach.