Chavez died this week at age 58 after a two-year battle with cancer, devastating millions of mostly poor supporters who hailed him for plowing Venezuela's vast oil wealth into social projects, but giving hope to foes who decried him as a dictator.
A frequent visitor to Caracas and fellow "anti-imperialist," Ahmadinejad received a standing ovation as he took his place in a guard of honor by Chavez's coffin, then broke protocol to touch the casket and clench his fist in a revolutionary salute.
Commander, here you are undefeated, pure, living for all time," Chavez's preferred successor, acting President Nicolas Maduro, said over the casket, his voice cracking with emotion.
"Your soul and spirit are so powerful that your body could not hold them, and now they are traveling this universe, growing with blessings and love."
The mourners chanted: "Chavez lives! The fight continues!"