El Salvador ordered Venezuela's diplomats to leave the country in a challenge to President Nicolas Maduro, prompting his government to respond by expelling Salvadoran envoys in Caracas on Sunday.

El Salvador under its new President Nayib Bukele is one of more than 50 countries that have declared Maduro's government illegitimate.

The country has switched its recognition to Maduro's lead rival, national assembly speaker Juan Guaido, who has declared himself Venezuela's acting president.

Bukele said El Salvador had ordered "the diplomatic corps from the regime of Nicolas Maduro" to leave the country within 48 hours, in a statement posted on his Twitter account late Saturday.

In response, the Venezuelan foreign ministry said in a statement on Sunday that it had declared each of the Salvadoran diplomats in Caracas "persona non grata" and gave them 48 hours to leave.

Maduro's leftist government has jailed opposition leaders and is accused of using torture and arbitrary arrests as it struggles to hold on to power amid a collapsing economy.

The President of El Salvador Nayib Bukele has granted Venezuelan diplomats 48 hours to leave the country
The President of El Salvador Nayib Bukele has granted Venezuelan diplomats 48 hours to leave the country AFP / Nicholas Kamm

But his government still has support from Russia and China.

On a visit to Cuba, Maduro shot back at the Salvadoran leader, saying: "You can't save yourself from history, Bukele."

"Your place in history as a traitor and puppet of the United States awaits you," Maduro said.

Before his election in June, Bukele said he would maintain a "distant" relationship with Caracas and close ties with the United States, Maduro's biggest diplomatic foe.

US Ambassador Ronald Johnson reacted warmly to El Salvador's decision.

"We applaud the government of President Nayib Bukele for ensuring that El Salvador is on the right side of history," he said on Twitter.

US President Donald Trump was one of the first leaders to recognize Guaido when the opposition leader mounted a bid to oust Maduro in April.