According to art. 5 of RD 557/2011, those non EU citizens who wants to enter Spain from a country outside Schengen area, 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 be valid for no more than ninety days from the expiration of the residence or stay permit, if requested prior to said expiration.
In the event that it is requested at a time after the expiration of the residence or stay permit, the return permit will be valid for no more than ninety days from the date it was granted.
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:
www.mpt.gob.es - Form EX_13
Return permit - Official Information Sheet (in Spanish)