The excavation and maintenance of buried natural gas pipelines can lead to deformation and stress redistribution of the pipelines and even cause secondary damage to the pipes with issues. To clarify the impact of excavation unloading on buried pipelines, this study established a finite element three-dimensional pipe-soil model, investigated the mechanical response of pipelines under layered excavation and evaluated various parameters impacting the response. The parameters analyzed include the diameter-thickness ratio of the pipe, excavation length and width, thickness of top covering soil, elastic modulus of soil, specific weight of soil and initial displacement of the pipeline. The study results showed that the pipeline bulges upwards during excavation unloading, the pipe top in the middle is under tension, and the bottom of the pipe is under compression. Therefore, the axial stress and vertical displacement both increase first and then decrease, and they are distributed symmetrically along the pipeline axis; excavating the initially compressed pipeline leads to high strain areas in the pipeline and even local buckling. The response to slope excavation is more pronounced than that to straight trench excavation; the additional response of the pipeline increases with the increase of diameter-thickness ratio, excavation width, thickness of pipe top covering soil and specific weight of soil, but it decreases with the increasing soil elastic modulus. The additional response is closely related to excavation length and the initial displacement. The results of this study can provide a reference for pipeline construction, maintenance, and safety assessment.