Zero-Phase Algorithm in Matlab
As far as I can see you're doing the basic filtering right. The difference with Matlab's filtfilt function is that the latter uses certain initial conditions in the calls to filter to minimize transients.

The Matlab documentation says the following:


  filtfilt minimizes start-up and ending transients by matching initial conditions, and you can use it for both real and complex inputs


The appropriate reference is


  Gustafsson, F. "Determining the initial states in forward-backward filtering." IEEE® Transactions on Signal Processing. Vol. 44, April 1996, pp. 988–992