The warning, via a series of tweets on Thursday, comes as a new caravan of more than 3,000 migrants from El Salvador, Honduras and Guatemala heads north.
Mr Trump has already threatened to cut all aid payments to the three countries over illegal immigration.
The group says they are fleeing violence and poverty.
“In addition to stopping all payments to these countries, which seem to have almost no control over their population, I must, in the strongest of terms, ask Mexico to stop this onslaught – and if unable to do so I will call up the U.S. Military and CLOSE OUR SOUTHERN BORDER!,” Mr Trump wrote.
The president has previously threatened to cut Honduran aid. The US sent more than $175m (£130m) to the country in 2016 and 2017, according to the US Agency for International Development.
Why does it concern Trump?
The group of Central American migrants has made its intentions clear: they are heading to the US.
Since he was on the campaign trail, Mr Trump has lambasted illegal immigrants, and this latest caravan comes after a major crackdown on migrants heading over the Mexican border.
Changes to detention rules saw thousands of migrant children detained and separated from their parents earlier this year, sparking national and international condemnation.
Mr Trump‘s recent comments are probably an effort to bolster support among his base by cracking down on migration.
High-stakes elections boost Trump threats
Analysis by Anthony Zurcher, BBC News
This isn’t the first time President Donald Trump has used an organised group of Central American refugees heading to the US to drum up support for his border security and immigration policies. With the mid-term elections 19 days away, however, the stakes are higher and the rhetoric is sharper.
There could be political benefit to the president’s accusations, lobbed toward Democrats, and the warnings to Latin American leaders – but presidential action would have consequences.
Using the military to “close” the border would be enormously disruptive in states like Texas and Arizona (which, by the way, have two high-profile Senate races). The move would also complicate relations between the US and Mexico, as Andres Manuel Lopez Obrador prepares to assume the Mexican presidency in December.
A recent Fox News poll shows that probable midterm voters are more interested in preserving healthcare protections and “reining in Donald Trump” than immigration or border security. The president, with his tweets, is trying to change that. If he can’t, it will be an election fought on uncomfortable ground for Republicans.
Where is the caravan now?
The migrant caravan is currently making its way through Guatemala, mostly on foot.
Their journey began in San Pedro Sula, Honduras, on Friday.
Most migrants are carrying few belongings, taking what they can carry in backpacks, as they head out on the nearly 2,800 mile (4,500km) trek from Honduras to the US border.
Could Trump close the US-Mexican border?
Thousands of people and goods legally cross the US-Mexico border daily.
While Mr Trump has sent national guard troops to the border before, it is unclear what he means by shutting it down entirely, and whether that would affect businesses or people with legitimate visas.
And according to international law, the US cannot deport asylum seekers without first determining the validity of their claim.
Under pressure from the US to stop the migrants, Mexico has sent federal police to the border, though they are not officially there to stop the caravan.
Mexican officials said on Wednesday that those without papers would have to apply for refugee status or turn back.
US Secretary of State Mike Pompeo will be in Mexico this Friday, and US media report that he will discuss plans to stop the caravan.
“The surreal side of this perceived crisis at the border is that the border is more under control now than it was 15 or 20 years ago,” Andrew Selee, president of the Migration Policy Institute, told the BBC.
Mr Selee said closing the border “would wreak havoc on Mexican and American economies”.
“It could be a symbolic effort,” he adds. “A way of pressuring Mexico – but that would have a limited effect on illegal crossings and a huge effect on legal crossings.”
What will happen to the people?
The Associated Press reports that many migrants do not have passports and have been using national ID cards, which allow them to travel within Central America. Mexico, however, requires a passport at entry.
Human rights groups have criticised the US and Mexican response to the caravan.
Erika Guevara-Rosas of Amnesty International said in a statement: “Mexican authorities should not take a Trump approach treating people like a security threat.”
“These families deserve dignity and respect to ensure that no one is illegally returned to situations where they could risk serious harm due to violence.”
Mexican president-elect Andrés Manuel López Obrador plans to offer work visas to Central Americans when he takes office in December.
“It is a plan that we have, that anyone who wants to work in Mexico will have a work visa,” he said.
Mr Selee told the BBC he thinks Mexico will “try to defuse the crisis” the same way they did with the last migrant caravan: by giving some people legal status or the chance to apply for asylum and deporting others.
“The caravan will probably diminish before it reaches the US-Mexico border, but the whole episode will harden views on immigration in the US on both sides and make Mexico more wary about cooperating with the US,” he says.
Why are they leaving?
An estimated 10% of the population of Guatemala, El Salvador and Honduras have fled danger, forced gang recruitment and dismal economic opportunities.
The region has one of the highest murder rates in the world. The UN reported murder rates in 2015 in Honduras standing at 63.75 deaths per 100,000 and El Salvador at 108.64 deaths.
Jari Dixon, an opposition politician in Honduras, tweeted on Monday that the caravan was not “seeking the American dream” but “fleeing the Honduras nightmare”.
In a statement on Tuesday, Honduras’ foreign ministry urged its citizens to “not let themselves be used by a movement that is clearly political and seeks to disrupt the governability, stability and peace”.