The current study aims to assess associations between trimethylamine N-oxide (TMAO) levels and mortality and to investigate modification effects of genetics. A total of 500 participants from a family-based cohort study were enrolled from 2005 to 2017 and followed up until 2020 in Fangshan District, Beijing, China. Serum TMAO levels were measured using the ELISA kit. The primary outcomes were all-cause mortality and deaths from CVD and stroke. During a median follow-up time of 7·38 years, thirty-eight deaths were recorded, including twenty deaths due to CVD and nineteen deaths due to stroke. Compared with the lowest TMAO quartile group, the HR for all-cause mortality was 1·35 (95 % CI: 0·44, 4·15), 1·65 (95 % CI: 0·58, 4·64) and 2·45 (95 % CI: 0·91, 6·57), respectively, in higher groups. No association was observed between TMAO and CVD mortality. However, compared with the lowest TMAO concentration group, the HR for stroke mortality was 1·93 (95 % CI: 0·40, 9·39), 1·91 (95 % CI: 0·41, 8·96) and 4·16 (95 % CI: 0·94, 18·52), respectively, in higher groups (Pfor trend = 0·046). Furthermore, polygenic risk score (PRS) for longevity modified the association of TMAO with all-cause mortality (Pfor interaction = 0·008). The risk of mortality (HR = 2·20, 95 % CI: 1·06, 4·57) was higher among participants with lower PRS compared with higher PRS (HR = 1·00, 95 % CI: 0·71, 1·40). The study indicates that elevated serum TMAO levels are potentially associated with long-term mortality risk in rural areas of northern China, especially for stroke deaths. Additionally, it provides novel evidence that genetic variations might modify the association.