Objective To determine the prevalence of Anaplasma marginale (A. marginale) and Anaplasma ovis from sheep and goat in different highland pasture in west of Iran. Methods From July 2015 to October 2015, 370 blood samples of sheep and goat were collected from different regions in Hamedan province, Iran. The DNA extracted from blood and subsequently, 16S rRNA and MSP4 genes were analyzed by nested-PCR, semi nested-PCR and RFLP methods. Results In the PCR assessment, overall 27.5% (102/370) of sheep and goat were positive for Anaplasma ovis and A. marginale infection, which was lower than reports from tropical and subtropical regions of Iran. Statistical analysis (the Chi-square test) did not show any significant relation between infection and variables such as location, tick infestation age and sex (P > 0.05). No significant correlation between the altitude and the Anaplasma species infection was found (Mann–Whitney test: P > 0.05). However, Anaplasma infection in goat significantly is more than the sheep (P = 0.008). Conclusions The ecological changes affect the frequency and distribution of Anaplasma species. Furthermore, our results indicate that sheep as potential reservoirs of A. marginale.