The Portuguese government could soon end 25 consecutive years of budget deficits — a historic moment for the indebted economy. Portugal has been in financial trouble since the sovereign debt crisis of 2011. Several years of economic mismanagement led to very high debt levels and raised market fears that the country would not be able to repay its loans. The difficulties in getting money from the market forced Portugal to ask for financial assistance that same year. But four years after ending a bailout program, the Portuguese government is now forecasting a budget surplus — when state expenditure is lower than revenues. According to the Finance Ministry, there will be a surplus of 0.25 percent of gross domestic product (GDP) in 2020 — a year ahead of schedule. This is, according to the European statistics office Eurostat, an accomplishment that the southern European nation has never managed since records began in 1995. Eurostat will confirm the final 2017 figures in late April.

Analysts claim the Portuguese projections are "realistic" for 2020 as Lisbon has managed to reduce its deficit over the last few years. But cautioned against potential risks. "Portugal shows today its best economic and financial performance in several decades," Finance Minister Mario Centeno said in late March, when presenting the current state of the country's finances.

What's the recipe for success?

A worker inspects Helix cork stoppers, which can be opened by hand and re-sealed, during quality control checks at the Amorim & Irmaos SA, factory, Paulo Duarte | Bloomberg | Getty Images

"The upgrade of Portugal's credit rating by S&P and Fitch, combined with the already-large reduction in Portugal's budget deficit and its exit from the EU's fiscal watchlist, are reducing Portugal's borrowing costs and interest payments, and this will help it reach its budget target sooner," Ben Robinson, senior economist at the think tank OMFIF (Official Monetary and Financial Institutions Forum), told CNBC in an email. Data from the Portuguese Finance Ministry showed that the country paid less than 300 million euros ($368.49 million) in interest on its sovereign debt between 2016 and 2017 due to the increasingly optimistic views from the ratings agencies.

The improvements in Portugal's general budget balance have been rather remarkable. Maartje Wijffelaars Economist at Rabobank

Fitch and S&P joined DBRS at the end of 2017 in giving Portugal's debt an "investment grade" after years of considering it "junk." They recognized that after the bailout program, which ended in 2014, the economy has recovered and is set to continue growing over the coming years. The employment rate has improved as well as its debt levels, the credit rating agencies said at the time. Portugal's structural deficit — which does not include one-off hits — is expected to be 1 percent of GDP in 2017, according to the latest government figures. In 2016, this had been 1.8 percent of GDP. Such an improvement has made investors more confident on Portugal's ability to repay its debts.

'Remarkable' Centeno

"The strong improvement of the budget balance in 2017 was to a large extent the result of the improving economic environment," Maartje Wijffelaars, an economist at Rabobank, told CNBC via email. Such improvement, she said, "has led to higher social contributions and tax revenues and lower spending on unemployment benefits for example."

Portugal grew 2.7 percent in 2017, according to data from the Finance Ministry. The positive momentum across the euro zone boosted confidence among the Portuguese. However, employment growth — it rose 3.2 percent in 2017, high foreign investment in the real estate market and strong tourism figures have also led to a strong domestic demand. "Centeno and his government should get credit," Wijffelaars said about the way Portugal has managed to keep a cap on the increase in public sector pay. "The improvements in Portugal's general budget balance have been rather remarkable," she said. "Going forward it's important that the government does more to also improve the structural budget balance. It can't depend on everlasting strong economic growth to improve its fiscal balance."

Too early to claim 'mission accomplished'

The Portuguese economy has improved but analysts believe there are still several risks that cannot be excluded. "Structural reform has been quite slow," Robinson from OMFIF told CNBC, in reference to the past few years following the end of the bailout and when the ruling socialist party took office. When compared with the rest of the euro zone, Portugal still has one of the highest levels of non-performing loans in its banking sector. This ratio stood at 14.6 percent in the third quarter of 2017; from 17.7 percent in the same period of 2016.

Horacio Villalobos | Corbis | Corbis | Getty Images