Breastfeeding self-efficacy (BSE), defined as a mother’s confidence in her ability to breastfeed, has been confirmed to predict the uptake of exclusive breastfeeding (EBF). Early experiences during the birth hospital stay, especially in-hospital formula feeding (IHFF), can impact both EBF and maternal breastfeeding confidence. Therefore, our objective was to examine the association between IHFF and EBF outcomes and investigate whether this association is influenced by BSE. The study included 778 infants from a larger cohort study conducted in 2021, with a one-year follow-up in rural areas of Sichuan Province, China. We used a causal mediation analysis to estimate the total effect (TE), natural direct (NDE), and nature indirect effects (NIE) using the paramed command in Stata. Causal mediation analyses revealed that IHFF was negatively associated with EBF (TE odds ratio = 0.47; 95% CI, 0.29 to 0.76); 28% of this association was mediated by BSE. In the subgroup analysis, there were no significant differences in the effects between parity subgroups, as well as between infant delivery subgroups. Our study found that IHFF hindered later EBF and that BSE mediated this association. Limiting the occurrence of in-hospital formula feeding or improving maternal breastfeeding self-efficacy is likely to improve exclusive breastfeeding outcomes.