Background: Neuroimaging reveals structural brain changes linked with HIV infection and related neurocognitive disorders; however, group-level comparisons between people with HIV and people without HIV do not account for within-group heterogeneity. The aim of this study was to quantify the effects of comorbidities such as cardiovascular disease and adverse social determinants of health on brain ageing in people with HIV and people without HIV. Methods: In this retrospective case-control study, people with HIV from Washington University in St Louis, MO, USA, and people without HIV identified through community organisations or the Research Participant Registry were clinically characterised and underwent 3-Tesla T1-weighted MRI between Dec 3, 2008, and Oct 4, 2022. Exclusion criteria were established by a combination of self-reports and medical records. DeepBrainNet, a publicly available machine learning algorithm, was applied to estimate brain-predicted age from MRI for people with HIV and people without HIV. The brain-age gap, defined as the difference between brain-predicted age and true chronological age, was modelled as a function of clinical, comorbid, and social factors by use of linear regression. Variables were first examined singly for associations with brain-age gap, then combined into multivariate models with best-subsets variable selection. Findings: In people with HIV (mean age 44·8 years [SD 15·5]; 78% [296 of 379] male; 69%  Black; 78%  undetectable viral load), brain-age gap was associated with Framingham cardiovascular risk score (p=0·0034), detectable viral load (>50 copies per mL; p=0·0023), and hepatitis C co-infection (p=0·0065). After variable selection, the final model for people with HIV retained Framingham score, hepatitis C, and added unemployment (p=0·0015). Educational achievement assayed by reading proficiency was linked with reduced brain-age gap (p=0·016) for people without HIV but not for people with HIV, indicating a potential resilience factor. When people with HIV and people without HIV were modelled jointly, selection resulted in a model containing cardiovascular risk (p=0·0039), hepatitis C (p=0·037), Area Deprivation Index (p=0·033), and unemployment (p=0·00010). Male sex (p=0·078) and alcohol use history (p=0·090) were also included in the model but were not individually significant. Interpretation: Our findings indicate that comorbid and social determinants of health are associated with brain ageing in people with HIV, alongside traditional HIV metrics such as viral load and CD4 cell count, suggesting the need for a broadened clinical perspective on healthy ageing with HIV, with additional focus on comorbidities, lifestyle changes, and social factors. Funding: National Institute of Mental Health, National Institute of Nursing Research, and National Institute of Drug Abuse.