Yep MM is correct, you are not going to be able to do this until you have your residents card. The UK driving license is not a problem.
With regards to the transfer duties and costs, they are not that high and are related to the size of the bike you buy in the end. The main problem is going to traffic at Arturo Soria
You will need half a day and if you are short on spanish it is very, very confusing. My advice if this is the case is to find a gestoria and pay them to do it for you.
The minimum insurance for riding the bike is 3rd party. I paid 600 euros a year for a 1000 cc, but with 4 years no claims discount. Generally the cheapest insurers I have found are AMV.
The most important piece of advice I want to give is regards to health insurance however. Basically make sure you have some, or pay extra for it on the policy you take out. I recently wrote of the aforementioned bike
and found my policy didn't cover the medical costs, and spanish hospitals look to get it back in traffic accidents. Fortunately I had the equivalent of what used to be the UK E111 health service cover so I was covered through that, without it I would have been looking at a large bill. I also had a private travel insurance policy which in the small print said I wasn't covered for riding bikes over 125cc, so check this out too if you have such a policy. Whatever you do, make sure you have it. If you ride in Madrid, you are likely to come off at some point.
I'm sure you know the old saying :- There are two types of motorcyclists, those who have had an accident...... and those who are going to