With 60 per cent of ballots counted, officials said Varela led with 39 per cent of the votes, compared to 32 per cent for former Housing Minister Jose Domingo Arias, the preferred choice of Martinelli. Juan Carlos Navarro, a former mayor of the capital, was in third in the seven-candidate field with 27 per cent.
Varela, who takes office July 1, dedicated his victory to Panama's democracy when the Electoral Tribunal's chief magistrate notified him by telephone of his victory.
Election day began with opinion polls pointing to a tight race among the top three candidates, but none of the major surveys had Varela with a lead. Most gave a razor-thin edge to Arias.
Although Martinelli wasn't on the ballot, the billionaire supermarket magnate's presence loomed large during the campaign, with many worried that he would be the power behind the throne if voters chose Arias, a soft-spoken newcomer.
Also Read
As the race narrowed in recent weeks, Martinelli crisscrossed the isthmus inaugurating hospitals, stadiums and Central America's first subway while warning the 3.2 million Panamanians that record-low unemployment and economic growth averaging more than 8 per cent since he took office in 2009 could be jeopardised if his opponents won.
Varela, a 50-year-old engineer, is the scion of one of Panama's richest families and owner of a namesake rum distillery. He left the 2009 presidential race to throw his conservative Panamenista party's support behind Martinelli in exchange for the vice presidency.
But the political marriage didn't last, and Martinelli dismissed him from an additional role as foreign minister in 2011 for refusing to back a plan for a referendum to allow president's to serve consecutive terms.