In this study, nitrogen-doped mesoporous carbon (NMC) and boron-doped mesoporous carbon (BMC) are synthesized and used as Pt-free counter electrode (CE) materials for dye-sensitized solar cells (DSCs). The optimized NMC and BMC CEs exhibit significantly higher catalytic activities than the pristine mesoporous carbon (MC). The I-/I3- in acetonitrile liquid electrolyte-based DSCs have power conversion efficiencies (PCE) of 7.20% with the NMC CE and 7.15% with the BMC CE, which are significantly higher than that obtained using the MC CE (5.88%). This is attributed to the improved effective catalytic active sites and the enhanced electrical conductivities of the NMC and BMC CEs. When Pt with trace amounts (0.1 wt%) is loaded on NMC, the liquid DSCs using Pt/NMC composite CEs yield PCE values of 8.31% and 9.92%, respectively, under 1.0 Sun and 0.1 Sun illumination. The Pt/NMC CE is further employed in quasi-solid DSCs, which also give a high PCE of 8.15% under 1.0 Sun illumination. A flexible Pt/NMC CE is also prepared on indium tin oxide-polyethylene naphthalate (ITO-PEN) substrate, and the generated liquid DSCs show a PCE of 6.92%. This work provides a feasible path to enhance the catalytic activity of the MC CE in DSCs.