The goal maintained Barcelona's perfect start to the Spanish league. In England, Liverpool convincingly beat Tottenham while Arsenal slumped to its second draw in three matches.
The best of the action from Europe's top leagues Sunday:
Barcelona was held in check by Villarreal until the 82nd minute, when Messi took Neymar's pass and drew goalkeeper Sergio Asenjo off his line before picking out Ramirez to push the winner into the net.
Messi had previously been denied twice by the goal frame when Asenjo blocked his free kick onto the post in the 28th and his shot stuck the upright in the 76th. But he delivered when hemmed in by Asenjo and a defender on the right side of the goal, squeezing a low pass under the goalkeeper for the streaking Ramirez to tap home from close range.
Neymar came on as a second-half substitute for his first competitive game since fracturing a vertebra at the World Cup. He missed last weekend's league opener with a sprained left ankle.
Barcelona has two wins in two games, and Real Madrid tried to keep pace when it played at Real Sociedad in a late game.
Liverpool had the look of Premier League title challengers again during a 3-0 win at Tottenham, while Arsenal's scoring struggles were once more exposed in a 1-1 draw at Leicester.
Alexis Sanchez scored in the 20th minute, his first league goal for Arsenal since joining from Barcelona. The goal was offset within two minutes by Leonardo Ulloa's header for newly promoted Leicester.
Liverpool, though, recovered from its setback at Manchester City at the start of the week with a convincing performance at Tottenham. Newly signed striker Mario Balotelli made his debut, but the goals came from forward Raheem Sterling, captain Steven Gerrard's penalty and new defender Alberto Moreno.
Unbeaten Aston Villa beat Hull 2-1 for its second win in three games.
Filippo Inzaghi got off to a winning start as AC Milan's coach, his team easing to a 3-1 victory over Lazio in its Serie A opener.
Goals from Keisuke Honda and Sulley Muntari and a penalty from Jeremy Menez gave Milan a comfortable lead before Lazio pulled one back with an own goal from new Rossoneri signing Alex.
New Milan goalkeeper Diego Lopez saved Antonio Candreva's penalty kick in stoppage time.
It was a convincing performance by Milan, which showed how little it missed Balotelli after his move to Liverpool last week. Balotelli's replacement, Fernando Torres, was watching in the stands at San Siro after completing his two-year loan move from Chelsea.
Also, Atalanta drew 0-0 at home to Hellas Verona.
Bordeaux dropped its first points this season by drawing with Bastia 1-1 in the French league.
Bastia striker Gadji Tallo pounced on a loose ball to score with a low strike in the 18th minute before Bordeaux forward Diego Rolan tied it by firing into the bottom corner in the 79th.
Defending champion Paris Saint-Germain hosted Saint-Etienne in a late game.