Kaposi sarcoma-associated herpesvirus (KSHV) causes Kaposi sarcoma (KS). The risk of KS is amplified in HIV-immunosuppressed individuals and antiretroviral therapy (ART) reduces KS incidence. Reliable data on the relationship between these factors are lacking in Africa. We used questionnaires and serum from 7886 black South Africans (18-74 years) with incident cancer, recruited between 1995 and 2016. ART rollout started in 2004. We measured associations between KS, HIV-1 and KSHV before and after ART rollout. We measured seropositivity to HIV-1, KSHV latency-associated nuclear antigen (LANA) and glycoprotein (K8.1) and calculated case-control-adjusted odds ratios (OR adj) and 95% confidence intervals (CI) in relation to KS and KSHV infection, before (1995-2004), early (2005-2009) and late (2010-2016) ART rollout periods. KSHV seropositivity among 1237 KS cases was 98%. Among 6649 controls, KSHV seropositivity was higher in males (OR adj = 1.4 [95%CI 1.23-1.52]), in persons with HIV, (OR adj = 4.2 [95%CI 3.74-4.73]) and lower in high school leavers (OR adj = 0.7 [95%CI 0.59-0.83]). KSHV seropositivity declined over the three ART rollout periods (37%, 28% and 28%, P trend <.001) coinciding with increases in high school leavers over the same periods (46%, 58% and 67%, P trend <.001). HIV-1 seroprevalence increased from 10% in the pre-ART period to 22% in the late ART period (P trend <.001). Compared to HIV-1 and KSHV seronegatives, KSHV seropositives yielded an OR for KS of 26 (95%CI 11-62) in HIV-1 seronegative participants and an OR of 2501 (95%CI 1083-5776) in HIV-1 seropositive participants. HIV-1 increases the risk of KS in those infected with KSHV by 100-fold. Declines in KSHV seroprevalence coincide with ART rollout and with improvements in educational standards and general hygiene.