Madrid is a relative safe city. Pickpocketing, esp. on tourists, is however frequent. But violent crime is very rare. SafeAround gives Madrid a Safety Index of 71%, in line with many other touristic European cities — for comparison purposes: Paris has 68%, London 70%, Berlin 78%, Rome 64%, New York City 67%, Tokyo 80%, and Shanghai 66%. A snapshot of the most relevant warnings and dangers is given below:
The Wikitravel Page for Madrid says:
Madrid is a relatively safe city. The police are visible, and the city is equipped with cameras. There are always a lot of people in the streets, even at night time, so you can walk across the city generally without fear. Travelers who remain aware of their surroundings, and keep an eye on their belongings should have little to worry about.
Madrid has a significant amount of non-violent pickpocket crime so always watch any bags you have with you especially on the Metro and in busier public spaces. Be careful when carrying luggage, especially if anyone approaches you with an outspread map in hand asking for directions. This may very possibly be a trap to distract you while an accomplice steals your luggage.
Beware of anyone who approaches you and asks you to write down your signature: it is normally for a “sick hospital” unit, and she will point out the “stamp” on the paper. She will then ask for a generous donation of €20 or more. This distraction can often be used to block vision while a pickpocketing or theft attempt is made.
Here is a nice list with some safety precautions.