The high incidence of atrial fibrillation (AFib) following cardiac surgery (postoperative atrial fibrillation, POAF) relies on specific surgical features. However, in the setting of POAF, the role of the microbiome in the modulation of cardiac fibrosis is still not clear. This study aimed to analyze the effect of the microbiome and its main metabolic product (trimethylamine-N-oxide, TMAO) in the fibrosis of myocardial tissue, to investigate its role in POAF. Patients undergoing elective cardiac surgery with cardiopulmonary bypass, central atrio-caval cannulation and no history of AFib, were included. A fragment of the right atrium was analyzed for qualitative and mRNA-quantitative evaluation. A preoperative blood sample was analyzed with enzyme-linked immunosorbent assay (ELISA). A total of 100 patients have been included, with POAF occurring in 38%. Histologically, a higher degree of fibrosis, angiogenesis and inflammation has been observed in POAF. Quantitative evaluation showed increased mRNA expression of collagen-1, collagen-3, fibronectin, and transforming growth factor beta (TGFb) in the POAF group. ELISA analysis showed higher levels of TMAO, lipopolysaccharide and TGFb in POAF, with similar levels of sP-selectin and zonulin. TMAO ≥ 61.8 ng/mL (odds ratio, OR 2.88 [1.35-6.16], p = 0.006), preoperative hemoglobin < 13.1 g/dL (OR 2.37 [1.07-5.24], p = 0.033) and impaired right ventricular function (OR 2.38 [1.17-4.83], p = 0.017) were independent predictors of POAF. Also, TMAO was significantly associated with POAF by means of increased fibrosis. Gut microbiome product TMAO is crucial for myocardial fibrosis, which is a key factor for POAF. Patients in preoperative sinus rhythm who will develop POAF have increased genetic expression of pro-fibrotic genes and enhanced fibrosis in histological staining. Elevated TMAO level (≥61.8 ng/mL) is an independent risk factor for POAF.