A long-running student discipline case involving a Maryland boy who was suspended after chewing a pastry into the shape of a gun and aiming it at his classmates has ended with a settlement between school officials and the child’s family.

Neither side would discuss the terms of the agreement, but the recent resolution brought closure to a 3 1 /2-year struggle that inspired legislative efforts concerning student discipline in several states and came to be known as the “Pop-Tart” case.

On March 1, 2013, a 7-year-old allegedly misbehaved during the morning breakfast period in his class at Park Elementary School in Anne Arundel County.

“Look — I made a gun!” he yelled, showing off a nibbled pastry bar and pointing the imaginary weapon at students at nearby desks and in a hallway.

The boy, then a second-grader, was suspended for two days, which his family described as an overreaction. “It was harmless,” his father, William “B.J.” Welch, told The Washington Post in 2013. “It was a danish.”

The boy — who has attention-deficit hyperactivity disorder, according to his parents — served his suspension and the family filed an appeal, asking that the incident be cleared from his school records. Their request was turned down by the Anne Arundel schools superintendent, a hearing examiner, the county school board, the Maryland State Board of Education and a county Circuit Court judge.

The parents did not give up, and the school system did not back down. The boy who once wielded the strawberry-filled pastry grew older: He’s now 11 and in middle school.

The Welches have long argued that the incident unnecessarily tarnished the boy’s school files: His disciplinary referral uses the word “gun” four times.

“It’s a mark on his record for something that doesn’t need to be there,” his father said in an interview in June. “There’s just a lot of unknowns, and I don’t want something that could potentially debilitate his future.”

But the school system successfully argued that the case was never about a pastry or a gun, but rather an ongoing behavioral problem. They said that the boy disrupted the classroom repeatedly and that the suspension was a last resort.

“We had not been able to make him understand that he had to follow the rules,” Sandra Blondell, the principal of the Brooklyn Park school, said at a hearing in 2014 that lasted more than six hours. The incident, she said, was “probably the 15th or 20th time there was a classroom disruption.”

The Maryland State Board of Education sided with Anne Arundel in 2015, saying that the school had used “progressive intervention” and that the suspension was justified “based on the incident in question and the student’s history.”

The case came 11 weeks after the 2012 mass shooting at Sandy Hook Elementary School in Newtown, Conn., which claimed the lives of 20 children and six staff members and heightened sensitivities about guns, even imaginary ones. In the Washington region, several young children were suspended from school for pointing their fingers like guns or having play guns.

Florida lawmakers invoked the Maryland incident as they passed a “Pop-Tart” law to limit zero-tolerance at schools, including discipline for “brandishing partially consumed pastry” or other food to simulate a weapon.

The resolution of the case followed the family’s latest appeal, to the Maryland Court of Special Appeals. Both sides agreed to participate in mediation, and a judge-mediator, Thomas L. Craven, helped forge a settlement that was signed last week, said Robin Ficker, the family’s attorney.

“The matter has been settled,” Ficker said. He could not comment further under the terms of the settlement. Welch also said he was barred from making a comment.

Anne Arundel schools spokesman Bob Mosier confirmed the settlement but declined to comment further.

Kaitlin Banner, a senior staff attorney with the Advancement Project, a civil rights organization active on school discipline issues nationally, said that student misbehavior often points to deeper issues that need to be addressed and that an out-of-school suspension does not resolve such problems. She also expressed concerns about student missteps appearing on school records throughout a child’s academic career.

“This is an example of how far the overreach of school district policies can be,” Banner said.

State Senator J.B. Jennings (R-Baltimore County), who sponsored an unsuccessful bill aimed at limiting such disciplinary actions, said he was glad for the resolution.

“The fact that the family has had to spend 3 1 /2 years to do what they felt was right for their child is wrong,” he said.

Earlier this year, when the family lost an appeal in Circuit Court, Anne Arundel school officials said the decision in their favor showed that the school system acted appropriately.

“We have believed from the outset that the actions of the school staff were not only appropriate and consistent with Board of Education policies and school-system regulations, but in the best interests of all students,” said Mosier, the school system spokesman.

Ficker argued at the time that school leaders should be able to deal with a 7-year-old in-house. “It would be different if this was a 17-year-old and he was threatening physical harm,” he added.