To address the problems of saturated physical adsorption and limited nitrogen retention in the composting process of biochar. The effect of adding unmodified biochar (BC), phosphorus-modified biochar (P-BC), phosphorus-magnesium-modified biochar (P-Mg-BC) and iron-modified biochar (Fe-BC) on nitrogen conversion was investigated during the pig composting, based on the principle of struvite crystals and humic acid-iron-ammonium complex, making use of physical adsorption and chemical fixation. The microbial mechanisms of modified biochar to promote nitrogen retention were also revealed. The results indicated that the addition of modified biochar could increase the germination index of the product (1.42%~10.55%) compared to BC. Modified biochar loading with nutrients elements changed the specific surface area and functional groups. So the P-BC (P:4.22% loading) and P-Mg-BC (P:3.34% loading; Mg:0.88% loading) reduced nitrogen losses by 19.46% and 3.03% respectively through chemical fixation. The Fe-BC (Fe:1.90% loading) further reduced the nitrogen loss by 20.07% by chemical immobilization and physical adsorption. All modified biochar promoted the growth of bacteria with nitrification function (e.g. Actinomadura, Corynebacterium) and reduced NH3 emissions by 21.29%~29.90%. During the compost cooling and maturation periods, modified biochar increased the abundance of nrfA in the dissimilatory nitrate reduction to ammonia and nifH genes in the nitrogen retention process, enhancing the retention of NH4+. It also improved the relative abundance of denitrification genes (nirK, nirS, norB), resulting in significant emissions of N2O (177.73%~317.35%). Considering compost maturity and nitrogen loss control, biochar loading with phosphorus and iron could achieve better nitrogen retention during pig manure composting.