Treatment failure after therapy of pulmonary tuberculosis (TB) infections is an important challenge, especially when it coincides with de novo emergence of multi-drug-resistant TB (MDR-TB). We seek to explore possible causes why MDR-TB has been found to occur much more often in patients with a history of previous treatment. We develop a mathematical model of the replication of Mycobacterium tuberculosis within a patient reflecting the compartments of macrophages, granulomas, and open cavities as well as parameterizing the effects of drugs on the pathogen dynamics in these compartments. We use this model to study the influence of patient adherence to therapy and of common retreatment regimens on treatment outcome. As expected, the simulations show that treatment success increases with increasing adherence. However, treatment occasionally fails even under perfect adherence due to interpatient variability in pharmacological parameters. The risk of generating MDR de novo is highest between 40% and 80% adherence. Importantly, our simulations highlight the double-edged effect of retreatment: On the one hand, the recommended retreatment regimen increases the overall success rate compared to re-treating with the initial regimen. On the other hand, it increases the probability to accumulate more resistant genotypes. We conclude that treatment adherence is a key factor for a positive outcome, and that screening for resistant strains is advisable after treatment failure or relapse. 