The Garmin-Sharp rider crossed the finish line at Mentougou Miaofeng just two seconds ahead of a charging trio of Esteban Chaves (OricaÃ¢â¬âGreenEDGE), Gilbert and former world champion Rui Costa (Lampre-Merida).
Starting the day 11 seconds behind Gilbert in the general classification, Martin needed real time plus the BMC rider to finish further down the standings to take the red jersey.
"It's the way it is," said Martin at the finish.
"I'm just happy to win the stage. I've been twice second in stages of this race and second overall last year so it's just nice to be able to win a stage."
Martin's victory was set up by a long solo effort from team-mate, Ryder Hesjedal who attacked with around six kilometres to go on the final climb after an early break was reeled in and the selections began to take place.
"I was a bit surprised when Ryder went but he's had good legs all week as well and he had to try," explained Martin.
"With the headwind, it was the only way to do it."
Gilbert's form has been on show for all to see this week with the Belgian claiming victory on Stage 2. Martin explained that he was not surprised that Gilbert was able to make the final selection on the definitive climb.
"I knew it was going to be a headwind on the climb," Martin said.
"It's not that hard of a climb. It's a big chain ring for a lot of it and we're going at 30km/h so it's not really a climber's climb at all."
Gilbert will take a three second advantage over Martin into Tuesday's final stage with Chaves a further six seconds back. It's a lead that Gilbert is confident of holding on to.
"You never know, you can have a technical or something but I'm in a safe position I think," he said.
Gilbert said he was looking forward to tomorrow's final stage of the final edition of the Tour of Beijing, which starts from Tiananmen Square before 12 laps around the Bird's Nest.
"That's pretty special because I'm happy to be here," the Belgian explained. "At the beginning I was thinking 'I don't want to go there, it's a long trip and the season has been long and so on'. But now I'm there and I enjoy the race, it's a nice country when the sky is clear. It's a nice experience."
Gilbert was particularly looking forward to the stage start opposite the Forbidden City.
"It's great to have these roads closed for us," he said. "It's a big experience. It's a little bit like riding on the Champs Elysees with the Tour (de France)."
Stage 4: 157km, Yanqing - Mentougou Miaofeng Mountain
1 Daniel Martin (IRL) Garmin Sharp 4hr 12min 14sec
2 Esteban Chaves (COL) Orica-GreenEDGE 0:00:02
3 Philippe Gilbert (BRL) BMC
4 Rui Costa (POR) Lampre-Merida
5 Julian Arredondo (COL) Trek Factory Racing 0:00:10
6 Rinaldo Nocentini (ITA) Ag2r-La Mondiale
7 Warren Barguil (FRA) Giant-Shimano
8 Rigoberto Uran (COL) Omega Pharma-QuickStep
9 Sergey Chernetski (RUS) Katusha
10 David Lopez (ESP) Sky 0:00:13
1 Philippe Gilbert (BEL) BMC Racing Team 15hr 19min 47sec
2 Daniel Martin (IRL) Garmin Sharp 0:00:03
3 Esteban Chaves (COL) Orica-GreenEDGE 0:00:09
4 Rui Costa (POR) Lampre-Merida 0:00:11
5 Sergey Chernetski (RUS) Katusha 0:00:23
6 Warren Barguil (FRA) Giant-Shimano
7 Julian Arredondo (COL) Trek Factory Racing
8 Rinaldo Nocentini (ITA) Ag2r-La Mondiale
9 Rigoberto Uran (COL) Omega Pharma-QuickStep
10 Pieter Serry (BEL) Omega Pharma-QuickStep 0:00:26