WASHINGTON — A 28-year-old North Carolina man pleaded guilty Friday in federal court to firing a military-style assault rifle inside a Washington pizza restaurant as he investigated spurious claims in online articles that the pizzeria was at the center of a child sex-slave ring.

The man, Edgar Maddison Welch, of Salisbury, N.C., also admitted that he transported a firearm and ammunition across state lines, a federal charge. He faces up to two decades in prison on both felony charges, but prosecutors and defense lawyers agreed he should serve no more than a maximum of seven years.

Mr. Welch, who remains in custody, is scheduled to be sentenced in June.

According to prosecutors, Mr. Welch traveled on Dec. 4 from North Carolina to the Comet Ping Pong restaurant in Northwest Washington after reading unfounded online reports that the restaurant had been harboring child sex slaves as part of a child-abuse ring led by Hillary Clinton, then the Democratic nominee for president. Prosecutors said Mr. Welch also watched YouTube videos about the false stories.

The authorities said Mr. Welch told friends that he was “sacrificing the lives of a few for the lives of many” and “standing up against a corrupt system that kidnaps, tortures and rapes babies and children in our own backyard.” Prosecutors said his friends declined to help carry out his mission.