By Daniel Wallis and Mario Naranjo
CARACAS, March 8 (Reuters) - Iran's Mahmoud Ahmadinejad and Cuba's Raul Castro joined about 30 other heads of state at Friday's funeral for Hugo Chavez in an emotional farewell to the charismatic but divisive Venezuelan leader who changed the face of politics in South America.
Chavez died this week aged 58 after a two-year battle with cancer, devastating millions of mostly poor supporters who loved him for putting the country's vast oil wealth at their service, but giving hope to foes who saw him as a dictator.
A frequent visitor to Caracas and fellow "anti-imperalist," 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.
"It's as if the world has come together around Hugo Chavez," said Venezuelan Foreign Minister Elias Jaua.
"His voice and his thoughts reached far beyond our shores."
Chavez's preferred successor and acting president, Nicolas Maduro, laid a replica of the sword of 19th century independence leader Simon Bolivar on top of the coffin, which was draped in the country's red, yellow and blue flag.
A singer in a cowboy hat serenaded the mourners with music from Chavez's birthplace in Venezuela's "llanos" plains.
The late president's body is to be embalmed and shown "for eternity" -- similar to how Communist leaders Lenin, Stalin and Mao were treated after their deaths.
His remains will lie in state for an extra seven days to accommodate the millions of Venezuelans who still want to pay their last respects to a man who will be remembered as one of the world's most colorful and controversial populist leaders.
Huge crowds of "Chavistas" gathered from before dawn for the ceremony at a military academy where his body was lying in state. Many were dressed in the red of the ruling Socialist Party, carrying his picture and waving Venezuelan flags.
"I asked God to grant him life, not me," said Leonida Munoz, 73, wearing a T-shirt bearing the image of Chavez's eyes. "He deserves to live more than I do."
She said she had been waiting more than 26 hours to view the Chavez's coffin. More than 2 million people have filed past the casket since Wednesday, many sobbing, some saluting, and others crossing themselves.
In Caracas were most of Chavez's highest-profile Latin American friends, such as Ecuadorean President Rafael Correa and Brazil's former leader, Luiz Inacio Lula da Silva.
"Most importantly, he left undefeated," Castro said, referring to Chavez's four presidential election wins, among a string of other ballot victories in his 14-year rule.
"He was invincible. He left victorious and no one can take that away. It is fixed in history."
Celebrity mourners included U.S. civil rights leader the Rev. Jesse Jackson Sr., who led a prayer at the service, Oscar-winning U.S. actor and director Sean Penn, and renowned conductor Gustavo Dudamel.
Dudamel, who heads Venezuela's Simon Bolivar Symphony Orchestra and the Los Angeles Philharmonic, led musicians at the funeral playing classical numbers including the national anthem.
Belarusian President Alexander Lukashenko and Ahmadinejad were among the more controversial figures. Ahmadinejad has caused a storm back home for saying Chavez would be resurrected alongside Jesus Christ and a "hidden" imam who Shi'ite Muslims believe will rise up to bring world peace.
The United States was not sending senior officials.
Former U.S. Rep. William Delahunt and U.S. Rep. Gregory Meeks were attending, amid speculation of a possible post-Chavez rapprochement between the two ideologically opposed governments.
A government source said Chavez slipped into a coma on Monday before dying the following day of respiratory failure. The cancer had spread to his lungs, the source added.
Chavez never said what type of cancer he was suffering, and for privacy, chose to be mainly treated in Cuba.
His death paved the way for a new vote in the OPEC nation that boasts the world's biggest oil reserves. But it is unclear when the election will be held.
At the gates of the academy, activists handed out photos of Chavez along with printed quotes of his call for supporters to vote for Maduro should anything happen to him.The constitution stipulates that an election must be called within 30 days, but politicians say the electoral authorities may not be ready and there has been talk of a possible delay.
Maduro, 50, a former bus driver who became foreign minister and then vice president, looks certain to face opposition leader Henrique Capriles, 40, the centrist governor of Miranda state who lost to Chavez in last October's election. He will be sworn in as caretaker leader after the funeral on Friday.
The Supreme Court ruled on Friday that Maduro would not have to step down to campaign. Capriles called the decision a "constitutional fraud."
Opposition sources say the 30 or so political groupings making up the Democratic Unity coalition have again agreed to back Capriles, whose 44 percent vote share in 2012 was the best performance by any candidate against Chavez.
Contrasting with the outpouring of grief at the funeral, senior opposition figure Leopoldo Lopez cautioned that the post-Chavez era would not automatically bring a brighter future.
"The uncertainty goes on, as does the gross meddling by Cuba and the flagrant violation of the constitution. Our people continue to be overwhelmed by insecurity, inflation and food shortages," he said. "We call on all Venezuelans to join the struggle to build a peaceful way out of the crisis."
Two recent opinion polls gave Maduro a strong lead over Capriles, and Western investors and foreign diplomats are factoring in a probable win for Maduro and a continuation of "Chavista" policies, at least in the short term.
The latest survey, by respected local pollster Datanalisis, gave Maduro 46.4 percent versus 34.3 percent for Capriles. It was carried out on in mid-February, before Chavez's death.