In order to implement the dual-carbon strategy and achieve sustainable economic development, it is essential to guarantee environmental protection through the establishment of an effective environmental rule of law. This study employs a quasi-natural experiment, namely the establishment of environmental protection courts in China’s intermediate people’s courts, to investigate the impact on the ESG performance of A-share listed companies from 2010 to 2022. A double-difference model is utilized for this purpose. This study reveals that the specialization of environmental justice is an effective means of promoting the ESG performance of enterprises. The results of mechanism tests indicate that the specialization of environmental justice has a positive impact on the ESG performance of enterprises, primarily by enhancing external supervision and garnering greater media attention and analyst interest. Furthermore, heterogeneity analysis reveals that the influence of environmental justice specialization on corporate ESG performance is particularly pronounced in eastern regions, contexts characterized by high environmental uncertainty and heavily polluting enterprises. These findings offer invaluable insights into the development of environmental justice and the advancement of sustainable economic growth.