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.
The president's threats also come just weeks before the mid-term elections on 6 November, which could see Democrats unseating Republicans on Capitol Hill.
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.
It may not end up much more than rhetoric, however. Mr Trump's Twitter threats have, in the past, proven an unreliable indicator of future decisions.
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.
Read More (...)