Randomised comparative data on efficacy and safety of second-line antiretroviral therapy (ART) after failure of non-nucleoside reverse transcriptase inhibitors (NNRTIs) across diverse geographical settings are scarce. The aim of this study was to evaluate optimal second-line ART for people with HIV. D2EFT is a completed international, randomised, open-label, phase 3b/4 trial evaluating three second-line ART strategies in adults (aged ≥18 years) with HIV-1 for whom first-line NNRTI therapy has failed. The study was done at 28 sites across 14 countries in Asia, Africa, and Latin America. It was originally designed to compare recommended standard of care (ritonavir-boosted darunavir [800 mg darunavir plus 100 mg ritonavir once daily] plus two nucleoside reverse transcriptase inhibitors [NRTIs; dosed once or twice daily]) with a novel nucleoside sparing regimen of dolutegravir (50 mg once daily) with ritonavir-boosted darunavir. The study was adapted during the first year to add a third arm of dolutegravir (50 mg once daily) with fixed tenofovir disoproxil fumarate (300 mg once daily) plus either lamivudine (300 mg once daily) or emtricitabine (200 mg once daily). Participants were randomly assigned with a computer-generated, blocked randomisation scheme (block size of two) stratified by site, previous tenofovir disoproxil fumarate use, and HIV viral load. The trial was designed to evaluate non-inferiority of either interventional arm against standard of care for the primary outcome of virological suppression, as determined by HIV RNA load of less than 50 copies per mL at 48 weeks. The prespecified non-inferiority margin was 12%. Comparisons were made with a modified intention-to-treat population, including all participants randomly assigned but excluding administrative withdrawals. This study is registered with ClinicalTrials.gov, NCT03017872. 1190 individuals were screened; 828 participants were enrolled between Nov 1, 2017, and Dec 31, 2021. Two participants were unable to receive their assigned regimen for administrative reasons; and 826 participants were included in analyses. Median age was 39 years (IQR 33-46), and 450 (54%) participants were female. Baseline median CD4 count was 206 cells per μL (23-354) and median HIV RNA was 15 400 copies per mL (3600-65 986). The proportion of participants with HIV RNA of less than 50 copies per mL at 48 weeks was 194 (75%) of 257 in the ritonavir-boosted darunavir plus two NRTIs group, 222 (84%) of 264 in the ritonavir-boosted darunavir plus dolutegravir group, and 227 (78%) of 291 in the dolutegravir with tenofovir disoproxil fumarate plus either lamivudine or emtricitabine group. Compared with ritonavir-boosted darunavir plus two NRTIs, the difference in virological suppression was 8·6% (95% CI 1·7 to 15·5; p=0·016) for dolutegravir plus ritonavir-boosted darunavir and 6·7% (-1·2 to 14·4; p=0·093) for dolutegravir with tenofovir disoproxil fumarate plus either lamivudine or emtricitabine. Six deaths occurred, none of which were related to treatment. 19 pregnancies (11 livebirths) occurred with no congenital defects. In individuals experiencing failure of an NNRTI-based first-line ART, a switch to either dolutegravir plus ritonavir-boosted darunavir or dolutegravir with tenofovir disoproxil fumarate plus either lamivudine or emtricitabine, without universal access to genotyping, was non-inferior in achieving viral suppression compared with ritonavir-boosted darunavir plus two NRTIs. These global data support the most recent WHO treatment guidelines. UNITAID; National Institute of Allergy and Infectious Diseases, USA; National Health and Medical Research Council, Australia; ViiV Healthcare; and Janssen.