Cesare Prandelli's side had dominated the first half but was under pressure in the second until the Liverpool midfielder sealed victory with a shot which went in off Raul Albiol
Riccardo Montolivo gave Italy the lead after 11 minutes with an exquisite chip to score his first international goal, but Xabi Alonso levelled with a penalty late in the first half after the home side had missed a number of good chances.
Montolivo started in an advanced role in midfield, with Thiago Motta protecting the back four just behind Daniele De Rossi and Andrea Pirlo.
Bari-born striker Antonio Cassano was paired up front with Villarreal's Giuseppe Rossi.
Spain coach Vicente del Bosque opted for a 4-2-3-1 formation, meanwhile, with Fernando Torres alone up front, ahead of Andres Iniesta, David Silva and Santi Cazorla.
Behind them, Alonso was paired with Javi Martinez, whose Athletic Bilbao teammate Andoni Iraola began at right-back in place of the injured Sergio Ramos.
And Spain's defense, also featuring Real Madrid pair Alvaro Arbeloa and Raul Albiol, was forced onto the back foot early on as Italy dominated the early stages.
Italy almost took the lead after just four minutes, when full-back Domenico Criscito got forward and unleashed a thunderous left-footed angled drive which beat Iker Casillas but rebounded off the keeper's right-hand upright.
Criscito then turned provider, setting up Montolivo with a great pass and the Fiorentina man beat Casillas with a delightful chip which went in off the underside of the bar.
Italy looked good for its lead at that stage and Rossi should have extended the advantage after 32 minutes, when he latched onto another Criscito pass but saw his shot half-saved by Casillas and then hacked clear just short of the line by an alert Albiol.
Cazorla then blasted over at the other end moments later and the Malaga man played a part as Spain drew level.
The midfielder sent in a cross for Fernando Lllorente, on for the injured Torres after 15 minutes, and the striker was adjudged to have been brought down by Chiellini.
The referee pointed to the spot and Alonso beat Gianluigi Buffon with a shot straight down the middle to bring Spain level, albeit rather undeservedly at this stage.
Italy's lead was almost restored straight away at the other end, when Cassano got ahead of Iraola but saw his low shot saved by Casillas' right boot, and the AC Milan forward also came close with a header moments before the interval.
Thiago came on to make his full Spain debut after the break - in the city of his birth - as both sides made a host of changes in the second period.
Thiago, in fine form with both Barcelona and Spain's Under-21 European Championship winning side this summer, appealed for a penalty when Cristian Maggio inadvertently handled in the box.
Club teammate David Villa, another halftime entrant, had earlier fired over with a speculative lob from a full 40 yards out.
Llorente should have made it 2-1 to Spain later on, but was too slow to react and saw his shot deflect off a defender and wide of the target following Silva's slick cut-back on 62 minutes.
Buffon saved from Alonso at the second attempt as La Roja continued to dominate, but Italy still looked dangerous and substitute Mario Balotelli went close with a run and shot which sailed just over the bar.
Buffon denied Silva and Juan Mata saw his shot hit Llorente as Spain sought a winner.
But Italy scored next, albeit with an element of good fortune, when Alberto Aquilani's shot deflected in off Albiol, leaving Victor Valdes - who had replaced Casillas at the break - completely wrong-footed, and that proved enough to seal a valuable victory for Cesare Prandelli's men on the night.