Spain and France are currently the only two eurozone countries still under special budget supervision by Brussels for running up deficits in excess of an EU-imposed limit of 3.0 percent of gross domestic product (GDP).
But "Spain will come out of the excessive deficit procedure in 2018, and we can be close to a balanced budget in 2020," Rajoy told an end-of-year news conference.
Madrid plans to reduce its deficit ratio to 3.1 percent in 2017, then bring it back below the 3.0-percent limit in 2018 when -- according to the government's draft budget -- it will come out at 2.3 percent.
Countries which repeatedly break the 3.0-percent rule enshrined in EU treaties can face possible penalties and fines.
At the end of the third quarter of 2017, Spain's deficit stood at 1.56 percent of GDP, putting on course to reach the 3.1-percent target for the whole of 2017, the budget ministry had said Thursday.
According to the ministry, Spain reduced its deficit by around 35 percent in the first nine months of 2017 compared to the year-earlier period, against a backdrop of very dynamic economic growth.
Madrid is pencilling in growth of 3.1 percent for 2017, about double the eurozone average.
Nevertheless, given the political crisis in Catalonia, Spain has still not approved its budget for 2018, which is crucial to keeping its promises made to Brussels.
The Basque National Party, critical of Madrid, is currently refusing to support the draft budget, while the Rajoy government, a minority in the assembly, needs the votes of Basque deputies for it to be adopted.