Both frequencyencoding gradients and phaseencoding gradients do work in exactly the same way, but are used for different purposes.
All imaging gradients temporarily change the resonant frequencies of protons while the gradient is being applied. When the gradient is turned off, the protons go back to their original precession frequencies. However, these protons will have gained or lost phase relative to a reference state though they are now again resonating at the same frequency. They possess "memory" of their historical encounter with the gradient manifest by a permanent phase shift (Φ).

The magnitude of the accumulated phase shift (Φ) is proportional to the strength of the gradient (G) and the time (t) that gradient is applied, reflected in the equation Φ = γ • G • t. By convention, application of a gradient in an MR pulse timing diagram the gradient strength is often indicated by a rectangle, where the height of the rectangle represents gradient strength (G) and its width reflects time (t) the gradient has been applied. The area under the rectangle (G•t) is therefore proportional to the phase shift.
Advanced Discussion (show/hide)»
Although I frequently draw gradients as idealized rectangles, it is impossible to have an instantaneous change of gradient strength from zero to some arbitrary value. In real life, gradients are most frequently trapezoidal in shape with rise and fall times as described in prior Q&A's. Sometimes gradients have curvilinear shapes such as half or full sinusoids. Regardless of their exact shape the general principle still holds  the area under a gradient is proportional to accumulated phase.
Related Questions
How does frequencyencoding work?
How does frequencyencoding work?