Centre-back Martinez let fly from inside the centre circle catching Gorka Iraizoz off his line, the visiting keeper perhaps distracted after one of his defenders had just been carried off on a stretcher, and the ball dipped under the crossbar.
Spain striker Llorente had put Bilbao ahead, turning in the area to score in the 34th minute, before Martinez levelled just past the hour mark in a sun-soaked San Sebastian.
Llorente then struck a superb volley to restore Bilbao's advantage in the 71st and earned under-pressure Argentine coach Marcelo Bielsa a first league win this season.
Bilbao climbed out of the bottom three to five points from six games, two behind their north-coast neighbours in ninth.
Get the best features, fun and footballing frolics straight to your inbox every week.
Thank you for signing up to Four Four Two. You will receive a verification email shortly.
There was a problem. Please refresh the page and try again.