According to art. 5 of RD 557/2011, those non EU citizens who wants to enter Spain from a Schengen area country, while applying fot extension of his residence permit or applying for the new ID card for foreigners (TIE), will need to apply for a return permit.
The return permit will issued by the National Police Stations, you will need to apply for an appointment at this web site.
For more information see:
Return permit - Official Information Sheet (in Spanish)