Why Does the Median Filter Minimize the Absolute Value Error $L_1$ Cost Function?
Given a set of values $ {\left\{ {s}_{i} \right\}}_{i = 1}^{N} $, we're basically after:
$$ \arg \min_{x} \sum_{i = 1}^{N} \left| {s}_{i} - x \right| $$
One should notice that $ \frac{\mathrm{d} \left | x \right | }{\mathrm{d} x} = \operatorname{sign} \left( x \right) $ (Being more rigorous would say it is a Sub Gradient of the non smooth $ {L}_{1} $ Norm function).
Hence, deriving the sum above yields $ \sum_{i = 1}^{N} \operatorname{sign} \left( {s}_{i} - x \right) $.
This equals to zero only when the number of positive items equals the number of negative which happens when $ x = \operatorname{median} \left\{ {s}_{1}, {s}_{2}, \cdots, {s}_{N} \right\} $.
Remarks

One should notice that the median of a discrete group is not uniquely defined.
The median is not necessarily an item within the group.
Not every set can bring the Sub Gradient to vanish. Yet employing the Sub Gradient Method is guaranteed to converge to median.
It is not the optimal way to calculate the Median. It is given to give intuition about what's the median.