Scotland had a half-chance inside two minutes when Andrew Robertson delivered a cross onto the head of Steven Fletcher, but the Sunderland striker's effort dropped into the arms of Gibraltar goalkeeper Jamie Robba.
After nine minutes, Gibraltar almost took the lead in incredible fashion when Joseph Chipolina delivered a cross which almost caught out Scotland goalkeeper David Marshall at his near post, but the stopper managed to claw clear.
The lively Liam Walker then dragged an effort wide for the visitors during an end-to-end opening 15 minutes in Scotland. Matt Richie had a chance for the home team on the counter, but his effort was straight into the arms of Robba.
Scotland had a penalty after 17 minutes, however, when Robba brought Shaun Maloney to the ground inside the box. Maloney took charge of the resulting spot kick and buried his effort into the bottom corner.
Less than two minutes later, Gibraltar were level as Lee Casciaro finished into the bottom corner following a swift counter to cap a famous moment for the nation.
Scotland had a wonderful chance to regain the lead after 22 minutes, but Steven Naismith brought a smart save from Robba. The hosts did score their second after 29 minutes, however, when Steven Fletcher flicked a header into the bottom corner for his first Scotland goal since 2009.
The home side then forced another penalty after Naismith was felled by Lee Casciaro inside the box. Maloney again stepped up, and again scored.
Steven Fletcher then hit the post for Scotland, who were threatening to run away with the match deep in the first period and a fourth goal arrived after 39 minutes when Naismith turned a low cross from Ikechi Anya into the bottom corner.