If an overflow occurs, the well must be closed immediately. To ensure the safety of the drilling wellbore in ultra-deep wells, it is critical to analyze the transient flow process and pressure fluctuations of the fluid in the annular space of the overflow well shut-in. This paper investigates the pressure fluctuation model of multi-phase fluid in the annular space after overflow occurs during gas well drilling, analyzes the influence factors of multi-phase fluid's pressure wave velocity, and simulates and analyses pressure fluctuation changes in the annular space in light of the impact of overflow well shut-in on wellbore safety. The model's correctness is verified by comparing it to experimental data published in the literature. The model is developed and examined using the example of a genuine gas well in western China. According to the study, as soon as the BOP closes, pressure rapidly increases to a high of 6.09 MPa at 5.89 s before falling to a valley of 4.68 MPa at 35.36 s. When the mild shut-in time lasts 20 s, the wellhead annular pressure reaches a maximum of 5.8 MPa. If the wellhead equipment can withstand the annular space's first pressure wave, a hard shut-in is recommended. To optimize the overflow well shut-in system and ensure wellbore safety, the change in pressure in the wellhead annular space as a result of varied shut-in timings and drilling fluid displacement is studied.