Peru's former president and his wife have been taken into custody after a judge ordered them detained while prosecutors prepare charges against them for alleged money laundering.

Soon after the judge issued his order on Thursday night, Ollanta Humala and Nadine Heredia were driven to the courthouse under a heavy police escort.

Judge Richard Concepcion ordered them held for up to 18 months while they are investigated.

The ruling marked the second time that the judge has mandated jail time for an ex-president since a massive graft scandal in neighbouring Brazil rippled into Peru.

Prosecutor German Juarez cited testimony from former executives of Brazilian builder Odebrecht to accuse the couple of taking $3m in illegal campaign funds during the 2011 presidential election.

READ MORE: The scandal and tragedy of Lula's corruption conviction

Juarez also accused the pair of taking undeclared funds from the late Venezuelan leader Hugo Chavez during a previous, unsuccessful presidential bid.

"Here's a president who rose to the presidency and governed us with an electoral campaign built on illicit money. That's serious because it morally wounds society," Juarez said.

Humala, a former military officer who led Peru from 2011 to 2016, turned himself in with Heredia, the co-founder of his nationalist party, immediately after the ruling. Both denied wrongdoing and called their pre-trial detention unfair.

As they headed to the courthouse, Humala said in a Tweet: "This confirms the abuse of power which we will confront in defense of our rights and those of everyone."

Prosecutors filed the petition for the couple's detention earlier in the week, arguing they might flee Peru to evade justice.

In February, Judge Concepcion had ordered the arrest of another former Peruvian president, Alejandro Toledo, for related charges. Toledo is in the US fighting attempts by Peruvian authorities to have him deported to answer the charges.

Authorities across Latin America have been moving to charge officials accused of taking some $800m in bribes from Odebrecht. The company acknowledged the bribes when it signed a plea agreement in December with the US Justice Department.

The bribes include some $29m paid in Peru for projects built during the administrations of Toledo, Humala and former President Alan Garcia.

The same scandal has ensnared former Brazilian President Luiz Inacio Lula da Silva, who was convicted on Wednesday and sentenced to almost 10 years in jail for taking gifts from another Brazilian builder that along with Odebrecht paid bribes to politicians in exchange for government contracts.

Silva denies the accusation and will remain free while he appeals what he says are politically motivated charges.