BUG: Masked arrays and apply_over_axes

Masked arrays version of apply_over_axes did not apply
function correctly to arrays with non-trivial masks.

Fixes #4461.