Necrotic enteritis (NE), caused by Clostridium perfringens (C. perfringens), presents a challenge to the global broiler industry. Evidence suggests that Toll-like receptor (TLR) ligands can enhance the immune responses in chickens and protect them against infectious diseases. This study investigated the protective effects of TLR21 ligand class B CpG oligonucleotides (ODN) against NE in broiler chickens. On day 21 of age, chickens were injected with 50 or 100 μg CpG intramuscularly, and one group was injected with 50 μg CpG followed by a booster dose on day 22. Subsequently, birds were orally challenged with C. perfringens twice daily for three days, starting on day 22. On day 22, intestinal samples were collected for gene expression analysis. On day 25, all birds were euthanized, intestinal lesions were scored, and tissue samples were collected from the intestine for gene expression analysis, lymphocyte subset determination, and histomorphological analysis. Cecal contents were also collected for microbiome analysis. The results demonstrated that CpG pre-treatment, either at a single dose of 100 μg or two doses of 50 μg per bird, reduced lesion scores compared to the positive control. C. perfringens infection increased crypt depth in both the jejunum and ileum in the positive control group compared to both the CpG-treated group. At 22 days of age, CpG administration at doses of 100 μg per bird enhanced expression of TLR21, interleukin (IL)-2, CXCL8, IL-10, and interferon (IFN)-γ mRNA transcripts in both the jejunum and ileum. Additionally, at 25 days of age, the group pretreated with two doses of 50 μg of CpG per bird showed increased expression of all cytokines in both the jejunum and ileum compared to the control groups. The percentage of intestinal lymphocytes was not affected by CpG pre-treatment. However, CpG pretreatment at doses of 100 μg resulted in a higher abundance of the members of families Lactobacillaceae and Bacteroidaceae, which are crucial for maintaining gut health. In conclusion, our findings suggest that pretreatment of chickens with intramuscular administration of CpG may be effective in maintaining gut health during C. perfringens infection.