* Moody's says outlook reflects careful balance of risks
* Losing investment rating would have triggered bonds selloff
* Policy changes need to be sustained -Moody's
* Treasury welcomes ratings reprieve (Recats; adds details, quotes from Moody's, Treasury, analyst)
JOHANNESBURG, March 23 (Reuters) - Ratings agency Moody's on Friday affirmed South Africa's investment-grade credit rating and revised its credit outlook to stable from negative, saying the previous weakening of national institutions was gradually reversing which supported an economic recovery.
In a swift response, the South African Treasury welcomed the decision, but acknowledged the warning by Moody's that promises to improve political and policy uncertainty were seen as essential in holding on to the country's rating.
Moody's rates Pretoria's debt at 'Baa3', the lowest rung of investment grade.
The credit ratings agency said the recovery of institutions in Africa's most industrialized economy would - if sustained - kick-start the economy as well as provide a stabilization of fiscal strength.
"The confirmation of South Africa's ratings reflects Moody's view that the previous weakening of South Africa's institutions will gradually reverse under a more transparent and predictable policy framework," Moody's said in a statement.
The decision was largely expected as all but two of 18 economists surveyed by Reuters this week forecast that South Africa would avoid a credit rating downgrade.
Ahead of the decision, the rand currency and local bonds traded firmly on Friday, outpacing emerging market peers on anticipation of the reprieve by Moody's.
The Treasury said in a statement that the reprieve from Moodys showed that steady progress in meeting the objectives set out in new President Cyril Ramaphosa's "State of the Nation" address in February was key for the countrys economic and fiscal prospects to be sustained.
"To improve South Africas investment and economic prospects, the government continues to work diligently on practical steps to provide the necessary policy certainty," the treasury said.
South Africa's economic growth has shown signs of recovery, expanding by a surprise 3.1 percent in the fourth quarter, the highest rate since the second quarter of 2016.
A downgrade to a "junk" rating by Moody's would have seen South Africa removed from Citi's influential World Government Bond Index (WGBI), and could trigger up to 100 billion rand ($9 billion) in asset sales by foreign investors.
South African debt has already been dropped from one the widely used global bond indexes, the JPMorgan Emerging Market Bond Index Global.
"The changing of the outlook to 'stable' is important news. It suggests South Africas investment-grade rating is secure, for at least one rating agency," said Razia Khan, chief Africa economist at Standard Chartered.
"This considerably lessens the likelihood of World Government Bond Index exclusion. It lessens the risk that South Africa will be subject to significant outflows. It is positive for the currency," Khan said.
S&P Global Ratings downgraded South African local currency debt to 'BB+', or junk territory, in November, citing a deterioration in the country's economic outlook and public finances.
In April, Fitch downgraded the rating to sub-investment grade after Zuma abruptly fired Pravin Gordhan as finance minister, saying it would likely result in a change in economic policy direction. It kept both local and foreign currency credit ratings unchanged at BB+ in November, with a stable outlook.
The country has, however, seen a return of sorely needed investor confidence since Ramaphosa replaced scandal-plagued Jacob Zuma, who resigned in mid February on the orders of the ruling African National Congress party.
Ramaphosa has wasted little time in working to pull South Africa back from the brink.
He reshuffled his cabinet in February, sacking and demoting several Zuma allies and restored Nhlanhla Nene as finance minister - two years after Nene's sacking from the same role by Zuma.
Ramaphosa this week also suspended Zuma ally Tom Moyane as head of the revenue service.
The Treasury also took the politically risky decision in its 2018 budget to raise value added tax for the first time in 25 years was cheered by investors and ratings firms.
"The stable outlook reflects a careful balance of risks. The new administration faces equally significant opportunities and challenges," Moody's said. ($1 = 11.7353 S.African rand) (Editing by James Macharia and G Crosse)