The assailants opened fire at an army vehicle on the main road of the town of Qatan, killing the four soldiers on board, the official said.
He said the gunmen belonged to Al-Qaeda in the Arabian Peninsula, considered by Washington as the most dangerous affiliate of the jihadist network.
AQAP is active across several parts of Yemen, taking advantage of a collapse of central authority during a 2011 uprising that ousted veteran president Ali Abdullah Saleh.