The judge adopted the argument of the prosecutor's office which warned there is a risk that the former leader might flee the country.
"There is a concrete danger of fleeing," said Judge Juan Carlos Checkley.
Checkley said Castillo was captured on Dec. 7 when he was trying to reach the Mexican Embassy in the capital Lima to seek political asylum, which means "the danger of Castillo fleeing has not diminished but remains latent over time.”
Castillo was ousted and arrested last week after attempting to dissolve Congress and rule by decree in an effort to avoid impeachment over allegations of corruption.
His removal from office has sparked days of nationwide protests which have left at least 10 people dead.
Thousands of demonstrators have been arriving in Lima over the past few days, expressing their rejection of the new government and the need for immediate elections.
Protesters have blocked roads with rocks, logs and burned tires and the army has been deployed to protect public spaces that have been fiercely attacked across the country.
In response to the violent protests, the country’s new government led by President Dina Boluarte declared a 30-day nationwide state of emergency Wednesday, suspending the right to gather and move freely.
Although Boluarte proposed bringing elections forward by two years, she did not calm tensions which have escalated in a nation that has had six presidents since 2016.
Source: Anadolu Agency