Always, always extend if you can.
The thing about Morocco is that it has lots of fascinating cities, but Tangier (where folks go by ferry from Spain) is not one of them. It will take the better part of three precious vacation days to have less than a full day in Tangier. Why? Because the ferries do not depart from a Spanish coastal town you would otherwise be going to. It's a large time expenditure to get to a really mediocre city. The good news is that there are flights to Fes and Marrakech (two of the most interesting Moroccan cities) from many cities in Europe, so you don't have to tack on a few (inadequate) days in Morocco just because you're in Spain. You can combine a nice trip to, say, Fes and Marrakech with time in England, France, Germany, etc. So I urge you to forget about Morocco on this trip.
Spain has very many highly worthwhile destinations, but I don't label cities "musts". It really depends on what sorts of things you're interested in: museums (historical? art?), historic sights (what period?), architecture, outdoor activities, nightlife... You haven't told us what type of sightseeing/experiences most interest you, so I'll take a stab at this:
October should be a good time of year for Andalucia, which is extremely (unbearably?) hot in the summer. The three key destinations there are:
- Seville (largest city there, needs minimum 4 nights, could use more)
- Cordoba (ideally 2 nights; could use 3 if you want to relax a bit)
- Granada (minimum 2 nights, could definitely use 3)
Other excellent destinations in Andalucia include Malaga (attractive coastal city), Cadiz (historic coastal city), Jerez (sherry bodegas and horse shows), Ronda and white villages such as Arcos de la Frontera, Grazalema and Zahara de la Sierra. Those are just the most-visited, sight-rich towns. There are other worthwhile destinations, including the Renaissance cities of Ubeda and Baeza.
Outside Andalucia you have these excellent and relatively quick side trips from Madrid (though I've spent the night in all of them and thought the time was worthwhile):
- Toledo (historic capital)
- Segovia (aqueduct)
- Cuenca (ravine-hugging houses and a couple of good, small, modern-art museums)
- Salamanca (historic university city)
Between Barcelona and Madrid is Zaragoza, an interesting, relatively untouristy city.
Then there are San Sebastian and Bilbao in the Basque country and Santiago de Compostela and other towns in Galicia--but these northern-coastal areas take more time to reach and travel through, and their weather is more conducive to a mid-summer trip (and even then rain is frequent).
Valencia, on the eastern coast well south of Barcelona, is nice but to me wouldn't be included on a first visit to Spain of just a few weeks. However, it has very fast rail service from Madrid, and that train line goes through Cuenca.
The weather is likely to be best in Andalucia, and as I wrote above, that's an area you really, really don't want to visit in the summer.
Of the places I've mentioned, all except the white villages of Andalucia are readily manageable by train (usually) or major bus line. The trains along the Barcelona-Zaragoza-Madrid-Cordoba-Seville line are extremely fast, and these's also good service Cordoba-Malaga, Cordoba-Granada and Seville-Jerez-Cadiz. Going to the Basque Country and/or Galicia (and the interesting points in between) involves more travel time and less being-there time.