Mg-doped Al0.7Ga0.3N epilayers (∼1 μm) were grown on an AlN/sapphire template by metalorganic chemical vapor deposition and the electrical and optical properties of these epilayers were studied. For optimized Mg-doped Al0.7Ga0.3N epilayers, we have obtained a resistivity around 105 Ω cm at room temperature and confirmed p-type conduction at elevated temperatures(≫700 K) with a resistivity of about 40 Ω cm at 800 K. From the temperature dependent Hall effect measurement, the activation energy of Mg acceptor is found to be around 400 meV for Al0.7Ga0.3N alloy. The optimized Mg-doped Al0.7Ga0.3N epilayers have been incorporated into the deep-ultraviolet (UV) (λ≪300 nm) light-emitting diode (LED) structures as an electron blocking layer. An enhancement in the performance of the UV LEDs was obtained. LEDs with peak emission wavelengths at 280 nm were fabricated with a circular geometry (300 μm disk diameter). Output power reached 0.35 mW at 20 mA and 1.1 mW at 150 mA dc current. The importance of Mg-doped Al0.7Ga0.3N alloys to suppress the long-wavelength emission components in deep-UV LEDs and the fundamental limit for achieving p-type Al-rich AlGaN alloys are also discussed.