Lionel Messi netted a hat-trick as Barcelona beat 10-man Real Madrid 4-3 in an El Clasico thriller at the Bernabeu.