Chronic hepatitis B virus infection (HBV) infection appears to be associated with extrahepatic cancers. This study aims to evaluate the causality and evolutionary mechanism of chronic HBV infection and gastric cancer through Mendelian randomization (MR) analysis and bioinformatics analysis. We conducted 2-sample MR to investigate the causal relationship between chronic HBV infection and gastric cancer. We identified 5 independent genetic variants closely associated with exposure (chronic HBV infection) as instrumental variables in a sample of 1371 cases and 2938 controls of East Asian descent in Korea. The genome wide association study (GWAS) data for the outcome variable came from the Japanese Biobank. Bioinformatics analysis was used to explore the evolutionary mechanism of chronic HBV infection and gastric cancer. Differential expression analysis and weighted gene co-expression network analysis (WGCNA) were performed to identify key targets that are commonly associated with both diseases, and their biological functions were investigated. Multiple machine-learning models were employed to select hub genes. The MR analysis showed a positive causal relationship between chronic HBV infection and gastric cancer (IVW: OR = 1.165, 95% CI = 1.085-1.250, P < .001), and the result was robust in sensitivity analysis. According to the bioinformatics analysis, the 5 key targets were mainly enriched in Toll-like receptor signaling and PI3K-Akt signaling. Two hub genes, CXCL9 and COL6A2, were identified, and a high-performing predictive model was constructed. Chronic HBV infection is positively associated with gastric cancer, and the evolutionary mechanism may be related to Toll-like receptor signaling. Prospective studies are still needed to confirm these findings.