This study aims to explore how peer education influences men who have sex with men (MSM) to accompany friends to test and then affects their own testing, and to reveal the internal mechanism of peer education influencing HIV testing. A cross-sectional study among MSM in 11 cities in Shandong province, China was conducted to collect information on demographic characteristics, behavior, HIV-related knowledge, HIV testing history, and use of prevention services. Blood samples were tested for HIV status. Logistic regression was used for univariate and multivariate analyses, and R Mediation package was used for mediating effect analysis. Of 4,005 participants, 66.8% (2,674/4,005) had HIV testing in the last year, 50.6% (2,026/4,004) accompanied a friend for HIV testing, and 65.3% (2,614/4,005) received peer education in the last year. In multivariate logistic regression analysis, MSM who had anal sex with men in the past six months (adjusted odds ratio [AOR] = 1.772, 95% confidence interval [CI] = [1.438, 2.182]), had used drugs (AOR = 2.762, 95% CI = [2.264, 3.368]), received peer education in the past year (AOR = 2.412, 95% CI = [2.081, 2.796]), and accompanied a friend for testing in the past year (AOR = 3.077, 95% CI = [2.653, 3.569]) were more likely to take HIV testing. Receiving peer education in the past year could influence their own HIV testing in the past year by affecting their accompanying one’s friends to take the test in the past year (a*b = 0.889, 95% CI = [0.733, 1.053]). Peer education not only had a direct effect on HIV testing but also had an indirect effect on HIV testing by influencing accompanying friends for testing.