The in-situ nitrogen-doped porous carbon materials was synthesized using penicillin mycelial residue (PMR) as the sole endogenous nitrogen-rich precursor through a two-step process applied for CO2 adsorption. Precise control over the structural properties and CO2 adsorption performance of the porous carbon was achieved by varying the KOH dosage and activation temperature. The porous carbon prepared under mild activation conditions (600 ℃, KOH/BC = 1) exhibited high nitrogen content and abundant ultramicroporous structures (pore size ≤ 0.7 nm), and demonstrated outstanding adsorption performance. The CO2 adsorption capacities reached 6.55 mmol/g at 0 ℃ and 3.48 mmol/g at 25 ℃, showing promising CO2/N2 selectivity and robust cyclic adsorption stability. Quantitative structure–activity relationship analysis displayed a strong correlation between adsorption density, CO2/N2 selectivity, and nitrogen content. Density functional theory calculations further confirmed that nitrogen-doping, especially pyrrolic-N, serves as favorable structural sites for CO2 adsorption. Additionally, Grand Canonical Monte Carlo simulations verified that the ultramicroporous structures significantly enhances CO2 adsorption due to their strong affinity for CO2. This study provides an economic strategy for the preparation of in-situ nitrogen-doped porous carbon with enriched micropore using PMR, highlighting their promising performances in CO2 adsorption, synchronously extending the harmless treatment and resource utilization of PMR to demonstrate a sustainable approach for environmental remediation.