Los Blancos led 1-0 thanks to a fortuitous goal from Angel Di Maria just after half-time, but then spent the bulk of the second period being frustrated by some outstanding saves from Malaga goalkeeper Willy Caballero as they sought a second goal.
However, the clincher duly arrived in stoppage time. Cristiano Ronaldo, who had had a frustrating afternoon, crashed a spot-kick into the bottom corner after 76th-minute substitute Bale had chased a long ball before going to ground under a challenge from Weligton.
The win marked a solid start to a crucial week for Ancelotti's men, who now turn their attention to the midweek Champions League clash with Juventus before Saturday's first Clasico of the season against Barca at the Nou Camp.
Bale will be assessed over the next few days before a call is made on whether the world-record 100million euros acquisition from Tottenham, who has struggled for fitness so far this term following a less-than-full pre-season programme, will make the starting XI against Juve.
"Gareth Bale is fit to play," the Italian said at his post-match press conference.
"He has three days of training and then we will decide If he starts against Juventus."
Los Blancos had headed into this game five point adrift of Primera Division leaders Barcelona and Atletico Madrid, and with Ancelotti calling for improvement.
The 54-year-old declared himself content with the display, but urged his players not to rest on their laurels.
"We worked more defensively," he said. "Everyone was focused on defending. We pressed well and recovered quickly. This was the difference with other games. We did not give opportunities and we were well positioned on the field.
"It was a very good game. I liked the team's attitude. I hope we will play with the same intensity in the coming games.
"Now we face two important games against Juventus and Barcelona. Today I am happy. We have to continue and do better, but today the team did well. The week has started well."
Ancelotti also had kind words for Ronaldo, who made an apologetic gesture to the home fans seemingly in acknowledgement of a below-par performance.
"He was not happy because he wanted to score - he had opportunities," the Real boss said. "I told him that it was just not his day, but he showed his nerve to take the penalty because when you're not having a good day it would have been easy to fail from the spot."
Goalscorer Di Maria admitted his fellow Argentinian Caballero had proved a thorn in Real's side for much of the match.
"Caballero is in great form and has shown that against a great rival," the forward said on www.realmadrid.com. "I hope he has the opportunity to play with Argentina."
He added: "We played a great match - that's what the coach wanted. We tried to do everything as well as we could against a team that are in good form. We are doing things well and improving as a team.
"We are working hard every day, and at half-time the coach said he was happy with the job that we were doing and that he knew we could win the game."