River water quality continues to deteriorate under the coupled effects of climate change and human activities. Machine learning (ML) is a promising approach for analyzing water quality. Nevertheless, the spatiotemporal dynamics of river water quality and their potential mechanisms in changing environments remain incomprehensively understood through available ML-based researches. Here, we developed a ML-based framework integrating a self-organizing map (SOM) model with a random forest (RF) model. This framework was applied to simultaneously investigate the spatiotemporal patterns and potential drivers of river permanganate (CODMn), ammonia nitrogen (NH3-N), and total phosphorus (TP) dynamics across 34 sites from 2010 to 2020 in a coastal city threatened by deteriorating water environment in southeastern China. The sites were divided into two clusters in the spatial context with different water quality conditions. The year of 2015 for NH3-N and 2018 for CODMn and TP were identified as the key turning points of water quality variations. Features including sewage discharge, population dynamics, percentage of cultivated land, and fertilizer application contributed greatly to water quality deterioration. The increase in forest vegetation reflected by percentage of forest and leaf area index may improve water quality. The ML-based modeling framework demonstrated a promising way to address the spatiotemporal dynamics of river water quality, and provided insights for water management in a coastal city with intensifying human-nature interactions.