Vice President Juan Carlos Varela pulled off a surprise victory in Panama's presidential election Sunday, thwarting an attempt by former ally President Ricardo Martinelli to extend his grip on power by electing a hand-picked successor.

Varela led with 39 percent of the votes, compared to 32 percent for Martinelli's choice, political newcomer Jose Domingo Arias. Juan Carlos Navarro, a former mayor of the capital, was running third in the seven-candidate field with more than 83 percent of the ballots counted.

Varela, who takes office July 1, dedicated his victory to Panama's democracy. Waving the nation's flag at a rally to celebrate his victory, he vowed to put aside the partisan bickering of the past five years and put an end to corruption that worsened under Martinelli.

"Whoever wants to do business should grab their things and go to the private sector," he told a cheering crowd in a clear swipe at the outgoing president.

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.

A free-market conservative, the 50-year-old Varela also has strong social credentials. Before breaking with Martinelli in 2011, he was the architect of the government's popular "100 for 70" program that provides a $100 monthly stipend to Panamanians over age 70 without a pension or retirement benefits.

Although Martinelli wasn't on the ballot, the billionaire supermarket magnate's presence loomed large during the campaign. Many worried he would be the power behind the throne if voters chose Arias, a soft-spoken former housing minister, and even seek to change the constitution to eliminate a two-term cooling off period before becoming eligible again to seek the presidency.

As the race narrowed in recent weeks, Martinelli crisscrossed the isthmus inaugurating hospitals, stadiums and Central America's first subway while warning the 4 million Panamanians that record-low unemployment and economic growth that averaged more than 8 percent since he took office in 2009 could be jeopardized if his opponents won.

His use of the bully pulpit in defiance of a law against campaigning by sitting presidents drew widespread criticism, as did his decision to place his wife, Marta Linares, as Arias' running mate. Never since the United States' 1989 overthrow of military strongman Manuel Noriega has an incumbent party won re-election in Panama.

A stunned Martinelli reluctantly recognized the results. Hounded by cameras while arriving at Arias' campaign headquarters Sunday night, he offered up a snappish "May God help us" and said he would shelve plans for a quiet retirement and devote himself to opposing Varela's rule.

"I'm going to hit them hard because I know what they're like," he said, alternately blaming media bias and faulty polling for the loss. Voter turnout was a high 75 percent.

The outgoing president will have a strong platform from which to battle Varela: Candidates from his Democratic Center party were the biggest winners in congressional voting Sunday, taking an estimated 29 of 71 seats in the legislative assembly. Varela's alliance won just 12 seats, meaning he will almost have to negotiate with Navarro's center-left Democratic Revolutionary Party, which elected 22 representatives.

Varela, who studied engineering at the Georgia Institute of Technology, is the scion of one of Panama's richest families, owner of the Varela Hermanos 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.

Since then, Varela has been the president's fiercest critic, accusing him of taking kickbacks for a government radar system contract with Italy's Finmeccanica.

Martinelli denied the charges and in turn marginalized Varela from decision-making, never tiring of calling the vice president a parasite for collecting supposedly collecting a government paycheck without doing any work.

As campaigning turned ugly in the final stretch, Varela was engulfed by accusations that he had received payments from the daughter of a political ally convicted in the U.S. of laundering money for an illegal online gambling ring. Varela vigorously defended himself, accusing Martinelli of leaking the story to try and derail his campaign.

Varela, in a February interview, said he would continue Martinelli's drive to improve Panama's infrastructure, the linchpin of the president's 60 percent approval rating.

But he vowed to boost transparency, reflecting the perception that much of $15 billion spent on upgrades was lost to corruption and poor planning.

Follow us on twitter.com/foxnewslatino
Like us at facebook.com/foxnewslatino