* Economy grew 3.1 percent q/q in Q4
* Agriculture recovering from drought
* Rand, bonds firm after GDP data (Adds analyst, background)
PRETORIA, March 6 (Reuters) - South Africa's economy grew more than expected at the end of last year as agriculture and trade recovered, data showed on Tuesday, boosting its chances of avoiding a potentially debilitating credit ratings downgrade.
The easing of a drought boosted the above-forecast rise in GDP in the fourth quarter. However, economists said Cyril Ramaphosa's election as leader of the ruling African National Congress late in the quarter, and as president last month, has raised expectations that the country will make economic reforms and possibly keep its last investment grade rating.
Statistics South Africa said the economy grew 3.1 percent in October-December, the highest rate since the second quarter of 2016, after expanding by a revised 2.3 percent in the third quarter.
The growth beat market expectations of a quarter-on-quarter GDP expansion of 1.8 percent, according to a Reuters poll.
The economy of Africa's most industrialised country has struggled in recent years, weighed down by low business and consumer confidence amid political and policy uncertainty.
But investors' sentiment has improved since Ramaphosa replaced Jacob Zuma, who resigned as head of state following a long series of scandals.
The rand, which was largely flat before the release of the data, firmed more than 0.8 percent against the dollar to a session high of 11.7300/dollar. Government bonds also firmed.
Finance Minister Nhlanhla Nene said on Monday that growth forecasts were likely to be raised in October's medium-term budget as the government completes reforms to boost growth and stabilise ailing state companies.
Ratings firm Moody's - the only major agency that still ranks South African debt as investment grade - is due to publish its rating decision this month after placing the country on review for a downgrade. S&P Global Ratings and Fitch already rate South African debt as "junk".
The agriculture industry registered the highest growth at 37.5 percent quarter-on-quarter, although the expansion was slower than in July-September when the sector expanded 41.1 percent. The sector is recovering from the severe drought in 2016.
Trade recovered in the fourth quarter to expand 4.8 percent after shrinking 0.1 percent in the third, while manufacturing grew 4.3 percent, up from 3.7 percent.
"Primary industries had very robust growth and that was emanating from the amount of crops and harvest from agricultural and fisheries sector," Statistics South Africa Deputy Director General Joe de Beer said.
Gross domestic product rose 1.5 percent on an unadjusted year-on-year basis in the fourth quarter versus 1.3 percent in the third quarter. The economy grew by 1.3 percent in 2017 compared with a revised 0.6 percent in 2016.
"Ratings agencies are much more likely to focus on structural reforms, and the higher growth rate that now looks possible over the coming years," Standard Chartered Bank's Chief Africa Economist Razia Khan said.
"While not downplaying the economy's still-considerable challenges, this should create a sound base for future improvements in South Africa's rating." (Writing by Olivia Kumwenda-Mtambo Editing by James Macharia and David Stamp)