Preheat the oven to 375°F (190°C). Lightly grease a 9-by-13-inch baking dish with cooking spray or olive oil.
Bring a large pot of salted water to a boil. Add the ziti and cook for 1–2 minutes less than the package instructions for al dente pasta. Reserve 1/2 cup of pasta water, then drain the pasta.
Heat the olive oil in a large skillet over medium heat. Add the Italian sausage and cook for 6–8 minutes, breaking it into small crumbles until browned and cooked through.
Add the diced onion to the skillet and cook for 3–4 minutes until softened. Stir in the minced garlic and cook for another 30 seconds, until fragrant.
Add the marinara sauce, Italian seasoning, optional red pepper flakes, black pepper, and salt if needed. Stir well and simmer gently for 3–5 minutes. Add a little reserved pasta water if the sauce is too thick.
In a large bowl, combine the cooked pasta with the sausage and marinara mixture. Add the ricotta, 1 1/2 cups of mozzarella, and 1/4 cup of Parmesan. Gently stir until the ingredients are distributed throughout the pasta.
Transfer the mixture to the prepared baking dish and spread it into an even layer. Sprinkle the remaining 1/2 cup of mozzarella and 1/4 cup of Parmesan over the top.
Cover the baking dish loosely with aluminum foil and bake for 15 minutes. Remove the foil and bake for another 10–15 minutes, until the cheese is melted and lightly golden and the sauce is bubbling around the edges.
Remove the dish from the oven and let the casserole rest for 5–10 minutes. This helps the cheese and sauce settle, making the pasta easier to serve.
Garnish with chopped fresh basil or parsley if desired. Spoon into bowls and serve warm with garlic bread or a fresh green salad.