Preheat your oven to 375°F (190°C). Grease a 9x13 inch (23x33 cm) baking dish with cooking spray.
Cook the mostaccioli 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 ground beef and cook, breaking it up into small pieces, until it is browned and cooked through.
Add the chopped onion and minced garlic to the skillet, and cook until the onion is softened, about 3-4 minutes.
Add the crushed tomatoes, tomato sauce, dried basil, dried oregano, red pepper flakes, salt, and pepper to the skillet. Stir to combine and bring the mixture to a simmer.
Let the sauce simmer for about 15 minutes, stirring occasionally, until it has thickened slightly.
Add the cooked mostaccioli pasta to the skillet with the sauce, and stir to combine.
Transfer the pasta and sauce mixture to the prepared baking dish, and sprinkle the shredded mozzarella cheese and grated Parmesan cheese over the top.
Cover the baking dish with aluminum foil and bake for 20 minutes.
Remove the aluminum foil and bake for another 10-15 minutes, until the cheese is melted and bubbly and the edges are golden brown.
Let the baked mostaccioli cool for a few minutes before serving. Garnish with fresh basil, if desired.