Preheat your oven to 375°F (190°C). Cook the ziti pasta according to package instructions until al dente. Drain and set aside.
In a large skillet, heat the olive oil over medium heat. Add the diced onion and sauté for about 3-4 minutes until softened. Stir in the minced garlic and cook for an additional 1 minute.
Add the ground beef or Italian sausage to the skillet. Cook until browned and fully cooked, breaking it apart with a spoon, about 5-7 minutes. Drain excess fat if necessary.
Stir in the marinara sauce, oregano, basil, red pepper flakes (if using), salt, and black pepper. Let the mixture simmer for about 5 minutes.
In a separate bowl, combine the ricotta cheese, egg, and a pinch of salt and pepper. Mix until smooth.
In a large mixing bowl, combine the cooked ziti with the meat sauce mixture. Stir until the pasta is well coated.
Spread half of the ziti mixture in a greased 9x13-inch baking dish. Dollop half of the ricotta mixture over the pasta, then sprinkle with 1 cup of mozzarella cheese.
Layer the remaining ziti mixture on top, followed by the rest of the ricotta mixture and the remaining mozzarella cheese. Finish with the grated Parmesan cheese on top.
Cover the baking dish with aluminum foil and bake for 25 minutes. Remove the foil and bake for an additional 15 minutes, or until the cheese is bubbly and golden.
Let the baked ziti cool for 5-10 minutes before serving. Garnish with fresh basil if desired.