The legislation, approved by a large majority of lawmakers, is in response to recent terror attacks in Europe carried out by migrants, according to hardline Prime Minister Viktor Orban.
Speaking at a swearing-in of the latest contingent of some 450 new border police in Budapest on Tuesday, Orban called immigration "the Trojan horse of terrorism".
"If the world sees that we can defend our borders... Then no one will try to come to Hungary illegally," he added.
Anyone who passed through a "safe third country" including Serbia will be rejected, and any appeals against rejections will be fast-tracked into a three-day procedure.
More From This Section
Migrants whose applications are rejected may have to cover the costs of their own detention.
Hungary previously systematically detained all asylum applicants but suspended the practice in 2013 under pressure from Brussels, the UN refugee agency and the European Court of Human Rights.
The UNHCR said the legislation "violates Hungary's obligations under international and EU laws, and will have a terrible physical and psychological impact on women, children and men who have already greatly suffered."
Amnesty International said in a statement last month that the new rules "disregard EU guiding principles that it is forbidden to detain someone on the basis that they have claimed asylum".
The government says the new camps will comprise converted shipping containers built onto existing "transit zones" erected in 2015 at the southern border with Serbia.
It says the new legislation will protect both Hungarian and EU citizens, with migrants abusing the previous rules and leaving Hungary before a verdict is reached, exposing the bloc to the risk of terror attacks.