Optical-resolution photoacoustic microscopy can visualize wavelength-dependent optical absorption at the cellular level. However, this technique suffers from a limited depth of field due to the tight focus of the optical excitation beam, making it challenging to acquire high-resolution images of samples with uneven surfaces or high-quality volumetric images without z scanning. To overcome this limitation, we propose needle-shaped beam photoacoustic microscopy, which can extend the depth of field to around a 28-fold Rayleigh length via customized diffractive optical elements. These diffractive optical elements generate a needle-shaped beam with a well-maintained beam diameter, a uniform axial intensity distribution and negligible sidelobes. The advantage of using needle-shaped beam photoacoustic microscopy is demonstrated via both histology-like imaging of fresh slide-free organs using a 266 nm laser and in vivo mouse-brain vasculature imaging using a 532 nm laser. This approach provides new perspectives for slide-free intraoperative pathological imaging and in vivo organ-level imaging.