Dice the onion into 1/2-inch pieces, mince the garlic, and roughly chop the spinach. While you're prepping, preheat your oven to 425°F and grease a 9x13 inch baking dish. This prep work ensures everything is ready when you need it, so the cooking flows smoothly without interruption.
In a large skillet over medium-high heat, brown the Italian sausage (with casings removed) and diced onion together, breaking the sausage into small pieces as it cooks, until the meat is no longer pink and onion is softened, about 8-10 minutes. Add the minced garlic, salt, and Italian seasoning, then cook for 1 minute until fragrant. Pour in the marinara sauce and simmer for 5-10 minutes to allow the flavors to meld. I find that simmering the sauce with the sausage for a few minutes really lets the spices penetrate the sauce rather than having them taste one-dimensional.
While the sauce simmers, bring a large pot of salted water to a boil and cook the mostaccioli pasta 2 minutes less than the package directions to keep it al dente—it will continue cooking in the oven. Drain the pasta and set aside. Stir the chopped spinach and red pepper flakes into the sauce from Step 2, cooking until the spinach is wilted, about 2-3 minutes. The residual heat will soften the spinach without turning it dark and bitter.
In a medium bowl, combine the ricotta, 2 cups of freshly shredded mozzarella, 1/4 cup Parmesan, and the egg. Mix until evenly combined. I prefer using freshly shredded mozzarella over pre-shredded because it melts much more smoothly and creates a creamier, more cohesive cheese layer without any graininess.
Spread 1/2 cup of the meat sauce from Step 3 on the bottom of your prepared 9x13 inch baking dish. Layer half of the cooked pasta over the sauce, then spread half of the cheese mixture from Step 4 over the pasta. Add half of the remaining sauce (about 1.5 cups) on top. Layer the rest of the pasta, then the remaining cheese mixture, and finish with the final layer of sauce. Top the whole dish with the remaining 2 cups mozzarella and 1/4 cup Parmesan.
Bake the assembled mostaccioli in your preheated 425°F oven for 15-20 minutes, until the top is bubbly and the cheese is golden brown. Remove from the oven and let it rest for 5 minutes before serving—this allows the layers to set slightly so each spoonful holds together nicely instead of sliding around on the plate.