CARACAS, Venezuela — President Hugo Chávez of Venezuela died Tuesday afternoon after a struggle with cancer, the government announced, leaving behind a bitterly divided nation in the grip of a political crisis that grew more acute as he languished for weeks, silent and out of sight, in hospitals in Havana and Caracas.

Close to tears and his voice cracking, Vice President Nicolás Maduro said he and other officials had gone to the military hospital where Mr. Chávez was being treated, sequestered from the public, when “we received the hardest and most tragic information that we could transmit to our people.”

In short order, police officers and soldiers were highly visible as people ran through the streets, calling loved ones on cellphones, rushing to get home. Caracas, the capital, which had just received news that the government was throwing out two American military attachés it accused of sowing disorder, quickly became an enormous traffic jam. Stores and shopping malls abruptly closed.

As darkness fell, somber crowds congregated in the main square of Caracas and at the military hospital, with men and women crying openly in sadness and fear about what would come next.