Grigor Dimitrov produced the best match of his short ATP career to knock out fourth seed Marco Baghdatis in a three-set thriller at the BMW Open in Munich.