Stormwater detention ponds are essential stormwater management solutions that regulate the urban catchment discharge towards streams. Their purposes are to reduce the hydraulic load to avoid stream erosion, as well as to minimize the degradation of the natural waterbody by direct discharge of pollutants. Currently, static controllers are widely implemented for detention pond outflow regulation in engineering practice, i.e., the outflow discharge is capped at a fixed value. Such a passive discharge setting fails to exploit the full potential of the overall water system, hence further improvements are needed. We apply formal methods to synthesize (i.e., derive automatically) optimal active controllers. We model the stormwater detention pond, including the urban catchment area and the rain forecasts with its uncertainty, as hybrid Markov decision processes. Subsequently, we use the tool UppaalStratego to synthesize using Q-learning a control strategy maximizing the retention time for pollutant sedimentation (optimality) while also minimizing the duration of emergency overflow in the detention pond (safety). These strategies are synthesized for both an off-line and on-line settings. Simulation results for an existing pond show that UppaalStratego can learn optimal strategies that significantly reduce emergency overflows. For off-line controllers, a scenario with low rain periods shows a 26% improvement of pollutant sedimentation with respect to static control, and a scenario with high rain periods shows a reduction of overflow probability of 10%–19% for static control to lower than 5%, while pollutant sedimentation has only declined by 7% compared to static-control. For on-line controllers, one scenario with heavy rain shows a 95% overflow duration reduction and a 29% pollutant sedimentation improvement compared to static control.