Teachers in Los Angeles reached a deal with school officials Tuesday to end a strike that affected more than 600,000 students and raised questions about staffing and the future of the nation’s second-largest school system.

The accord was announced by Mayor Eric Garcetti, Superintendent Austin Beutner and Alex Caputo-Pearl, the president of the teachers union, after an all-night bargaining session. The teachers union late Tuesday approved the accord and announced that teachers will be back in classrooms Wednesday.

“All of us have a huge stake in our schools,” Garcetti said. “We have seen over the last few weeks the way the city has rallied around public education. Quite frankly, it’s been breathtaking.”

According to a summary of the pact released by the union, the district agreed to hire enough teachers to reduce average class size by four students by 2022, which will help relieve overcrowding in schools where classes regularly top 40 students. The district also assented to hiring 300 nurses over the next three years so that every school has a full-time nurse. It also committed to hiring a full-time librarian for every high school, and 17 more counselors. The deal includes a 6 percent pay raise.

Beutner said he still has “tremendous” concerns about the school system’s financial health but that those fears must be balanced with students’ needs.

“Today and tomorrow, when school opens, begins a new chapter in every classroom,” he said, noting that “40 years of underinvestment in public education” can’t be solved in one week.

Caputo-Pearl said teachers were expected to review and vote on the contract Tuesday.

“Educators and parents reached a boiling point,” Caputo-Pearl said. “It has brought us not only to an agreement but to a commitment to really fight for public education.”

Ricardo Vargas, a 17-year-old senior at Dorsey High, showed up at school Tuesday for the first time during the strike after remaining out last week because he heard students were just hanging out in the gymnasium. Vargas, who worked on a campaign to get more money for schools that have significant needs, said he was pleased officials were working to reduce class sizes. But he said he was unsure trimming classroom rosters by a couple of students would make much difference.

Still, he was eager to get back class.

“The break was nice,” he said. “But I was missing out on my education.”

About 30,000 teachers went on strike Jan. 14, vowing to remain out of classrooms and on picket lines until their demands for smaller class sizes and more support staff were satisfied. The teachers, having already secured a raise and free health care, focused their attention on staffing needs. They sought more instructors so that class sizes could be reduced and called for expanding the cadre of librarians and nurses, who rotate among schools. Educators also wanted more psychologists to better meet the needs of special-education students.

But the teachers and their union, United Teachers Los Angeles, also viewed the strike as a fight for the future of public education in Los Angeles, where charter schools have drawn students and resources away from traditional public schools. They view Beutner, a former investment banker and nonprofit organization executive who had never worked in public education, as being too accommodating to charter schools, which are independently run but publicly funded.

Beutner, who was hired last year to get the district’s finances in order, had maintained that the system could not afford the union’s demands. The Los Angeles Unified School District, like others across California, has contended with rising health-care costs and growing pension debt.

The strike in Los Angeles drew tens of thousands of teachers, parents and students to the streets, where they picketed in front of schools, rallied at city hall, and marched to the headquarters of the school district and the California Charter Schools Association.

The district kept schools open with skeleton staffs of parent volunteers, administrators and classroom aides.

The job action came a year after teachers in a half-dozen states walked off the job, protesting low pay and poor classroom conditions, and portends another year of teacher activism. About 350 miles north of Los Angeles, in Oakland, Calif., teachers have been preparing for the possibility of a strike as they continue to demand higher wages and more classrooms resources.

Teachers in Denver were set to vote Tuesday on whether to strike after an impasse with the district over salaries. They could strike as soon as Monday. In Virginia, teachers plan to stage a march Monday in Richmond, the state capital, to protest school funding cuts and low wages.

Valerie Strauss contributed to this report.