Preheat the oven to 375°F (190°C) and lightly coat a 9x13-inch casserole dish with nonstick spray or a small amount of oil.
Bring a large pot of salted water to a rolling boil. Cook the spaghetti until al dente, then drain thoroughly and toss with 1 tablespoon of olive oil to prevent sticking.
In a skillet over medium heat, sauté the diced onion in a tablespoon of olive oil until it turns translucent and tender. Stir in the minced garlic and cook for an additional 30 seconds until aromatic.
Pour the marinara sauce into the pan, incorporating the Italian seasoning. Season with salt and freshly ground pepper to taste. Stir in the cooked meatballs and let the mixture simmer gently for a few minutes.
To assemble, begin by layering half of the cooked spaghetti in the bottom of the prepared baking dish. Spoon over half of the meatball and sauce mixture, then sprinkle with half of the shredded mozzarella and a portion of the grated Parmesan.
Repeat the layering process with the remaining spaghetti, followed by the rest of the sauce and meatballs. Top with the remaining cheeses.
Cover the dish with aluminum foil and bake for 25 minutes. Remove the foil and continue baking for an additional 10 to 15 minutes, or until the cheese develops a golden, bubbling crust.
Allow the casserole to rest for 5 minutes after removing it from the oven. Garnish with chopped basil or parsley, if desired, and serve hot.