Jorge Molina nodded fifth-placed Betis in front after 19 minutes on a rain-soaked Benito Villamarin pitch in Seville before Francisco Silva levelled in the 73rd.
Castro then raced on to a superb through ball from substitute Dorlan Pabon with 11 minutes left and clipped the ball past keeper Andres Fernandez to claim his 13th league goal of the campaign.
Osasuna had Silva sent off for a second yellow card in the closing stages.
Betis are level on 43 points with fourth-placed Andalusian rivals Malaga who visit Real Valladolid on Saturday. Osasuna are sixth from bottom on 28 points.
"Our goal at the start of the season was to secure our position in the top flight," Castro told Marca TV. "We have achieved that and now we are going for a bigger prize."
Betis last took part in European competition in 2005-06 when they finished third in their Champions League group behind Liverpool and Chelsea.
They were relegated in 2008-09 before winning promotion back to the top flight two years later.
After two straight defeats to Real Madrid in the league and the Copa del Rey, Barcelona can open a 14-point lead over second-placed Atletico Madrid with a win at home to bottom club Deportivo La Coruna on Saturday.
On Sunday, Atletico host seventh-placed Real Sociedad while Real Madrid, in third spot, travel to third from bottom Celta Vigo.
Los Verdiblancos managed to secure the win despite the opposition having more shots.
Osasuna fired in nine shots, three of which were accurate, while Real Betis produced five shots, all of which were on target.
The most effective of Beticos's three substitutions was Pabón, who, after replacing Beñat in the 78th minute, assisted on Castro's goal.
Osasuna actually came out on top in terms of ball possession, posting a 60.9% possession rate during the contest.
Gorritxo's players linked up for 288 passes and 20 crosses.
Masoud Shojaei made his mark on the match for Osasuna after coming on for Kike Sola in the 45th minute, as he took three shots.