Firstly, the change in South African society from now compared to 40 years ago is vast in just about every way. Consider the fact that Black South Africans were brutally subjugated right up until the end of apartheid, and now the country is ruled by the ANC, and has implemented Black Economic Empowerment and other policies to help lift disadvantaged groups out of poverty. A similar story can be seen in the transition towards gay rights in a democratic South Africa, with the post-apartheid constitution specifically accounting for the equality of peoples regardless of sexual orientation (an interesting account of SA's decriminalisation of homosexuality can be found here). It is also imperative to remember how many figures in the South African gay rights movement were heavily involved with anti-apartheid activism, and therefore, the gay rights movement in many circles came to be strongly associated with the struggle against apartheid (although there was an interesting split in the gay rights movement, where some LGBT rights campaigners aligned themselves with the National Party, most of them were at least sympathetic to the ANC, and the gay rights movement was heavily white-dominated).
So in many ways South Africa's early legalisation of gay marriage was the result of this early constitutional commitment to LGBT rights. But, with South Africa, one must remember that the law of the land is often miles away from the realities of ordinary people. Legally, South Africa may lead the continent on gay rights, but LGBT people's lives are blighted by violence and discrimination, and the adoption of same-sex marriage was a court-led result of legal activism more so than it was of popular activism. The corrective rape of gay women is a horrifically common practice, and homophobic violence is one of those morbid realities of modern South Africa. It is worth bearing in mind just how big the difference is between progressive laws and an often very homophobic segment of the population. But to answer your question, the radical shift in gay rights in South Africa was the logical conclusion of a radical shift in human rights and the end of apartheid.
Edit: I couldn't remember the name of the book I read which details South Africa's progress on gay rights, but I've dug it up, and it's called To Have and to Hold: The Making of Same-Sex Marriage in South Africa, if you can get a copy from a library it's a really fascinating read!