Preheat the oven to 190 degrees Celsius and lightly grease a 23 x 33 centimeter baking dish.
Bring a large pot of salted water to a boil and cook the spaghetti until just shy of al dente according to the package directions. Reserve 120 milliliters of the pasta water, then drain the spaghetti.
While the pasta cooks, heat the olive oil in a large skillet over medium heat. Add the diced onion and cook for 4 to 5 minutes until softened.
Add the ground beef to the skillet and cook for 6 to 8 minutes, breaking it up with a spoon, until browned and fully cooked. Drain excess fat if needed.
Stir in 3 cloves of the minced garlic and cook for 30 seconds until fragrant. Add the marinara sauce, tomato sauce, Italian seasoning, salt, black pepper, and red pepper flakes. Simmer for 8 minutes, stirring occasionally.
In a large bowl or the empty pasta pot, melt the butter and stir in the remaining 1 clove minced garlic. Add the drained spaghetti and toss well to coat. Add a splash of reserved pasta water if needed to loosen the noodles.
Spread half of the garlic butter spaghetti into the prepared baking dish. Spoon over half of the beef sauce, then sprinkle with 120 grams of the mozzarella and 30 grams of the Parmesan.
Layer the remaining spaghetti over the first layer, then top with the rest of the beef sauce. Finish with the remaining 120 grams mozzarella and 30 grams Parmesan.
Cover the dish loosely with foil and bake for 20 minutes. Remove the foil and bake for 10 more minutes until the cheese is melted and the edges are bubbling.
Let the baked spaghetti rest for 5 minutes before serving. Sprinkle with chopped parsley and serve hot.