In major depressive disorder (MDD) patients, nonsuicidal self-injury (NSSI) is a common comorbidity, and it is important to clarify the underlying neurobiology. Here, we investigated the association of NSSI with brain function and structure in MDD patients. A total of 260 MDD patients and 132 healthy controls (HCs) underwent resting-state functional magnetic resonance imaging and three-dimensional T1-weighted structural scans. NSSI behaviour was assessed through interviews. Voxel-based morphometry analysis (VBM), regional homogeneity analysis (ReHo), functional connectome topology properties and network-based statistics were used to detect the differences in neuroimaging characteristics. Finally, the random forest method was used to evaluate whether these factors could predict NSSI in MDD. Compared with HCs, MDD patients with a history of NSSI showed significant right putamen grey matter volume (GMV), right superior orbital frontal cortex ReHo, left pallidum degree centrality, and putamen-centre function network differences. Compared to MDD subjects without NSSI, those with past NSSI showed significant right superior temporal gyrus (STG) GMV, right lingual gyrus ReHo, sigma and global efficiency, and cerebellum-centre function network differences. The right STG GMV and cerebellum-centre function network were more important than other factors in predicting NSSI behaviour in MDD. MDD patients with a history of NSSI have dysregulated spontaneous brain activity and structure in regions related to emotions, pain regulation, and the somatosensory system. Importantly, right STG GMV and cerebellar loops may play important roles in NSSI in MDD patients.