You cannot see Andalucia (much less Madrid and Andalucia) from a single base.
Malaga is a perfectly nice place, but with only six nights, I think very few people would say it makes the cut.
Seville is larger than Cordoba and Granada, and its sights are scattered. It needs more time--a minimum of 3 nights as far as I'm concerned, and the only reason I'm not specifying 4 nights is that you just don't have enough time.
Seville and Granada are way beyond reasonable day-tripping distance. The Alhambra is a large complex, not a single building. Lots of people spend more than half a day there. A realistic minimum for the city is 1-1/2 days (2 nights), though more time would be warranted if you had it.
The thorny issue is how to handle Cordoba. Like Granada, it's worth 2 or more nights, but you don't have time to do that. Cordoba can fit anywhere in your itinerary, because you will pass through it on the train leaving Madrid and returning to Madrid. In many/most cases you'll also pass through it on the way from Seville to Granada, so take a look at the train schedule to see what fits with the way you like to structure your days (early starts? keep going until 10 PM?). There aren't a lot of fast trains in and out of Granada, so that's the first part of the schedule to consider.
There are three basic ways to arrange a visit to Cordoba:
Spend 1 or more nights there. This is ideal, because the city is a great place just for wandering around after the day-trippers go home.
Make Cordoba a day trip from Seville. The train takes as little as 41 minutes from Santa Justa Station, but you need to add extra time to get to Santa Justa from your Seville hotel. Not being encumbered by luggage means you can start sightseeing immediately when you hop off the train in Cordoba, and there would be no time spent checking out of your old hotel and into a new one in Cordoba.
See Cordoba in transit between two of the other cities. The in-transit visit minimizes time spent sitting on trains, but it has the disadvantage of requiring you to store your luggage in Cordoba and later retrieve it. I've never had to do that in Cordoba, but there are reportedly lockers at the bus station, which is very near the train station. Demand exceeds supply, so you'd need to have a back-up plan in case there were no lockers available by the time you arrived. Googling luggage storage Cordoba will turn up options, I'm sure; you'd want something near the train station. There aren't many trains to Granada, so consult the schedule before choosing to visit Cordoba on the way between Seville and Granada or Madrid and Granada. I'd also be cautious about planning an in-transit stop on my first full day in Europe, for fear I'd be really sluggish that morning and not want to take an early train, thereby cutting deeply into the time I planned to spend in Cordoba.
How to handle Cordoba really depends on your personal travel preferences--how you feel about one-night stops and getting luggage on and off trains. The order of travel is flexible, but this is the best I can come up with on the allocation of time:
Seville: 3 nights
Granada: 2 nights
Madrid: 1 night
Cordoba either as a day-trip or in-transit stop.
Something to consider: You really need to get out of Madrid on your arrival day. It's difficult to know what to do about scheduling a train trip on your arrival day, because your flight could be late or you might run into an unexpected hold-up at the airport. Fares tend to be high for tickets bought on the day of travel, yet the normal recommendation is to wait until you're at the destination before purchasing (and there's a train station right at the airport where you can do that). The trip to Cordoba is shorter and therefore cheaper than the trips all the way to Seville and Granada, so the penalty for buying Cordoba tickets after arrival wouldn't be as high.
Out of space--to be continued.