Cyclic codes are a subclass of linear codes and have wide applications in data storage systems, communication systems and consumer electronics due to their efficient encoding and decoding algorithms. Let p≥5 be an odd prime and m be a positive integer. Let C(1,e,s) denote the p-ary cyclic code with three nonzeros α, αe, and αs, where α is a generator of Fpm⁎, s=pm−12, and 2≤e≤pm−2. In this paper, by analyzing the solutions of certain equations over finite fields, we present four classes of optimal p-ary cyclic codes C(1,e,s) with parameters [pm−1,pm−2m−2,4]. Some known results on optimal quinary cyclic codes with parameters [5m−1,5m−2m−2,4] are special cases of our constructions. In addition, by analyzing the irreducible factors of certain polynomials over F5m, we present two classes of optimal quinary cyclic codes C(1,e,s).