Midfielder Ander Herrera put Bilbao ahead at San Mames stadium before Cesc Fabregas drew the visitors even with a header.
Bilbao appeared set to get the win after Gerard Pique scored an own goal in the 80th, but Messi slotted home his league-leading 14th goal in the 91st to split the points.
Barcelona remained undefeated in 18 games in all competitions to start the season.
Earlier, Cristiano Ronaldo scored his fourth hat trick in 11 league games this season as Madrid demolished Osasuna at home.
Short link: