Background: Across sub-Saharan Africa, men who have sex with men (MSM) and transgender women (TGW) have disproportionately poor HIV treatment outcomes. Stigma and criminalization create barriers to health-care engagement and adherence to antiretroviral therapy (ART), potentially promoting the development of HIV drug resistance (HIVDR). We evaluated transmitted, pre-treatment and acquired HIVDR among MSM and TGW in Lagos and Abuja, Nigeria. Methods: Adults with HIV RNA =1,000 copies/ml in the TRUST/RV368 cohort, including incident cases diagnosed via 3-monthly screening, underwent HIVDR testing using the Sanger sequencing method. Major mutations conferring resistance to nucleoside reverse transcriptase inhibitors (NRTIs), non-nucleoside reverse transcriptase inhibitors (NNRTIs) and protease inhibitors (PIs) were identified from the 2017 IAS-USA list. World Health Organization surveillance drug resistance mutations (SDRMs) were identified in ART-naive participants. Results: From March 2013 to June 2017, 415 participants with median age 24 (interquartile range [IQR] 21-27) years, CD4+ T-cell count 370 (IQR 272-502) cells/mm3, and HIV RNA 4.73 (IQR 4.26-5.15) log10 copies/ml underwent HIVDR testing. SDRMs were observed in 36 of 373 ARTnaive participants (9.7%, 95% confidence interval [95% CI 6.8, 13.1%]), including 8 of 39 incident cases (20.5%, [95% CI] 9.3, 36.5%). Among 42 ART-experienced participants, NNRTI resistance was detected in 18 (42.9%, 95% CI 27.7, 59.0%) and NRTI resistance in 10 (23.8%, 95% CI 12.0, 39.4%). No PI resistance was detected. Conclusions: The high prevalence of transmitted and acquired drug resistance among Nigerian MSM and TGW living with HIV suggests the need for programmatic solutions to improve uninterrupted access to ART and timely switch to second-line regimens in cases of viral failure.