Under a reform that came into place in September 2012, foreigners without residency papers lost their national health cards which allowed them free treatment in local public health clinics.
Illegal immigrants who are over 18 only get free treatment in case of an emergency, pregnancy or birth.
"It is more sensible and reasonable that this care be given in health centres so emergency wards are not saturated," Prime Minister Mariano Rajoy said as he announced that all undocumented immigrants would once again come within the fold of the public health system.
Amnesty International and other right groups had fiercely criticized the government's decision to deny public healthcare to illegal immigrants, saying it breached international agreements.
Health charity Doctors of the World ran a campaign called the "Right to treat", in which it encouraged doctors to continue treating illegal immigrants.
In a report issued at the end of 2013, the group said the reform had a "devastating effect" as over 800,0000 people had lost access to free public healthcare in just one year.
Several regional governments, including those of Andalusia and Catalonia, Spain's largest and most populous regions, ignored Madrid's orders and kept providing healthcare to illegal immigrants.
Rajoy said however that illegal immigrants will not have the right to a health card in line with policy in other European Union nations.
But he said the government would draw up nationwide rules to ensure illegal immigrants can access public healthcare.
Spain's College of Physicians welcomed the move, but it said that without a health card illegal immigrants will still not receive free medication.
"Their health care will be more like that of the 19th century and the 21st century," it said in a statement.
The government's reversal comes as Spain gears up for regional and local elections in May and a year-end general election.