Media playback is not supported on this device
Leicester's Selby, runner-up in 2007, was 10-5 behind but won 10 of the next 12 frames for a 15-12 lead.
Five-time champion O'Sullivan, who had never lost a world final, reduced the deficit to 15-14, only for Selby to win the next three frames.
Age: 30
Nickname: The Jester from Leicester
Turned pro: 1999
Ranking titles: World Championship 2014, UK Championship 2012, Shanghai Masters 2011, Welsh Open 2008
Non-ranking titles: Masters 2008, 2010, 2013
His victory means he returns to the top of the world rankings and leaves O'Sullivan two behind Stephen Hendry's record tally of seven world titles.
Selby dedicated his win to his late father, who died when he was 16.
"To beat Ronnie in the final to win my first world title is a dream come true," he added:
O'Sullivan said: "I tried my hardest but he was just too good,"
Selby, 30, is £300,000 richer for his Crucible triumph and is the ninth player to win the Triple Crown, having won three Masters titles and a UK Championship.
Selby looked jaded on Sunday following his epic semi-final against Neil Robertson but managed to win the final two frames to stay within three of O'Sullivan, who wrapped up his last-four encounter against Barry Hawkins with a session to spare.
He then won five of the six frames in Monday's opening session to drag his rival into unfamiliar territory, O'Sullivan having never before trailed heading into the final session of a Crucible final.
Selby made breaks of 55, 52 and 74 - his highest of the match so far - to level the contest at 10-10 before winning a tense 21st frame to lead for the first time.
O'Sullivan retook the lead after the mid-session interval, winning an error-strewn frame courtesy of a run of 50, but missed a simple pink into the middle pocket to clinch the frame after deciding to play it with pace.
Selby duly cleared up before punching the air and ramming his cue into the ground in a mixture of relief and elation.
Media playback is not supported on this device
Eight frames were scheduled for the session but it was a measure of the attritional nature of the action that they could only fit six in before the interval.
O'Sullivan won the first frame of the evening session with a fluent break of 100, his third century of the match, to level at 12-12, but Selby won a 28-minute frame to restore his advantage before opening up a two-frame lead for the first time.
Selby compiled a break of 56 to increase his lead to three frames before the mid-session interval, his run of take 10 of the last 12 frames.
O'Sullivan started well after the restart, rattling in breaks of 30 and 49 to reduce the gap to two frames. He then potted a magnificent yellow down the cushion before clearing up the rest of the balls to make it 15-14.
But Selby reacted brilliantly, knocking in a break of 127, his first century of the match, before a run of 87 to move within a frame of victory.
O'Sullivan had chances in the next frame but Selby pounced to complete a nerve-jangling clearance after his opponent missed the final red.
Mark Selby staged a remarkable comeback to beat defending champion Ronnie O'Sullivan 18-14 in the final of the World Championship in Sheffield.