Maria Sharapova secured a hat-trick of Stuttgart titles after coming from behind to beat Ana Ivanovic in Sunday's final.